export enum FieldType {
    text = 'text',
    textarea = 'textarea',
    html = 'html',
    email = 'email',
    number = 'number',
    integer = 'integer',
    date = 'date',
    select = 'select',
    bool = 'bool',
}

enum ResourceAction {
    create = 'create',
    read = 'read',
    readOne = 'readOne',
    update = 'update',
    delete = 'delete',
}

export enum Renderer {
    text = 'text',
    html = 'html',
    bool = 'bool',
    date = 'date',
    time = 'time',
    datetime = 'datetime',
}

const allResourceActions = [
    ResourceAction.create, ResourceAction.read, ResourceAction.readOne,
    ResourceAction.update, ResourceAction.delete,
]

export type SourceRef = {
    sourceName: string,
    fieldName?: string,
}

export type CobaltActionBase = {
    successKey?: string,
    resultKey?: string,
    data?: Record<string, any>,
    gather?: Record<string, SourceRef>,
}

export type CobaltAction = CobaltActionBase & (
    {
        type: 'route',
        route: string,
        method: string,
    }
    | {
        type: 'navigate',
        route: string,
    }
)

export type ActionButton = CobaltAction & {
    title: string,
    description?: string,
    color?: string,
    icon?: string,
}

export type LoadAction = {
    target: SourceRef,
    action: CobaltAction,
}

export type CobaltActionResult =
    {
        success: true,
        message?: string,
        result?: unknown,
    }
    | {
        success: false,
        message?: string,
        error?: unknown,
    }

export type OldCobaltAction = {
    slug: string,
    title: string,
    color: string,
    icon: string,
    type: 'route',
    route: string,
    overall?: boolean,
}

export type FieldBase = {
    key: string,
    display: string,
    type: FieldType,
    required: boolean,
    sort?: 'asc' | 'desc',
    renderer?: Renderer,
    readonly?: boolean,
    helpText?: string,
    placeholder?: string,
    queryable?: boolean,
    hideOn?: {
        form?: boolean,
        listing?: boolean,
    },
}

export type SelectOptions = {display: string, value: any, uuid?: string}[]

export type FieldDefinition = FieldBase
    | FieldBase & { type: FieldType.select, multiple?: boolean, options: SelectOptions }

export type FormConfig = {
    sourceName?: string,
    fields: FieldDefinition[],
}

export type ListConfig = {
    sourceName: string,
    rowKey: string,
    fields: FieldDefinition[],
    actions?: {
        top?: ActionButton[],
        inline?: ActionButton[],
    },
}

/*export interface ResourceConfiguration {
    key: string,
    source: DataSource,
    primaryKey: string,
    orderField?: string,
    orderDirection?: OrderDirection,
    generateKeyOnInsert?: () => string|number,
    processBeforeInsert?: (row: QueryRow) => Awaitable<QueryRow>,
    processAfterRead?: (row: QueryRow) => Awaitable<QueryRow>,
    display: {
        field?: string,
        singular: string,
        plural: string,
    },
    supportedActions: ResourceAction[],
    otherActions: CobaltAction[],
    fields: FieldDefinition[],
}*/

export { ResourceAction, allResourceActions }
