// src/types/field.d.ts

export enum FieldType {
    CALC = "CALC",
    CATEGORY = "CATEGORY",
    CHECK_BOX = "CHECK_BOX",
    CREATED_TIME = "CREATED_TIME",
    CREATOR = "CREATOR",
    DATE = "DATE",
    DATETIME = "DATETIME",
    DROP_DOWN = "DROP_DOWN",
    FILE = "FILE",
    GROUP = "GROUP",
    GROUP_SELECT = "GROUP_SELECT",
    LINK = "LINK",
    MODIFIER = "MODIFIER",
    MULTI_LINE_TEXT = "MULTI_LINE_TEXT",
    MULTI_SELECT = "MULTI_SELECT",
    NUMBER = "NUMBER",
    ORGANIZATION_SELECT = "ORGANIZATION_SELECT",
    RADIO_BUTTON = "RADIO_BUTTON",
    RECORD_NUMBER = "RECORD_NUMBER",
    REFERENCE_TABLE = "REFERENCE_TABLE",
    RICH_TEXT = "RICH_TEXT",
    SINGLE_LINE_TEXT = "SINGLE_LINE_TEXT",
    STATUS = "STATUS",
    STATUS_ASSIGNEE = "STATUS_ASSIGNEE",
    SUBTABLE = "SUBTABLE",
    TIME = "TIME",
    UPDATED_TIME = "UPDATED_TIME",
    USER_SELECT = "USER_SELECT"
}

export enum CalcFormat{
    NUMBER = "NUMBER",
    NUMBER_DIGIT = "NUMBER_DIGIT",
    DATETIME = "DATETIME",
    DATE = "DATE",
    TIME = "TIME",
    HOUR_MINUTE= "HOUR_MINUTE",
    DAY_HOUR_MINUTE= "DAY_HOUR_MINUTE"
}

export interface FieldForm {
    code: string;
    type: FieldType;
    required?: boolean;
    noLabel?: boolean;
}

export interface OptionForm{
    index: string;
    label: string;
}

export interface EntityForm {
    code: string;
    type: 'USER' | 'ORGANIZATION' | 'GROUP' | 'FUNCTION';
}

export interface CalcFieldForm extends FieldForm {
    unit?: string;
    expression?: string;
    format?:CalcFormat;
    unitPosition?: 'BEFORE' | 'AFTER';
    displayScale?: string;
    hideExpression?: boolean;
}

export interface CategoryFieldForm extends FieldForm {
    enabled?: boolean;
}

export interface DateFieldForm extends FieldForm {
    defaultValue?: string;
    unique?: boolean;
    defaultNowValue?: boolean;
}

export interface DatetimeFieldForm extends FieldForm {
    defaultValue?: string;
    unique?: boolean;
    defaultNowValue?: boolean;
}

export interface DecimalFieldForm extends FieldForm {
    maxValue?: string;
    defaultValue?: string;
    unitPosition?: 'BEFORE' | 'AFTER';
    minValue?: string;
    unit?: string;
    unique?: boolean;
    displayScale?: string;
    digit?: boolean;
}

export interface EditorFieldForm extends FieldForm {
    defaultValue?: string;
}

export interface EntityForm {
    code: string;
    type: 'USER' | 'ORGANIZATION' | 'GROUP' | 'FUNCTION';
}

export interface FieldMappingForm {
    relatedField: string;
    field: string;
}

export interface FileFieldForm extends FieldForm {
    thumbnailSize?: string;
}

export interface GroupFieldForm extends FieldForm {
    openGroup?: boolean;
}

export interface GroupSelectFieldForm extends FieldForm {
    entities: EntityForm[];
    defaultValue?: EntityForm[];
}

export interface LinkFieldForm extends FieldForm {
    protocol?: 'WEB' | 'CALL' | 'MAIL';
    defaultValue?: string;
    minLength?: string;
    unique?: boolean;
    maxLength?: string;
}

export interface LookupFieldForm extends FieldForm {
    lookup: {
        lookupPickerFields: string[];
        relatedKeyField: string;
        relatedApp: {
            app: string;
            code: string;
        };
        fieldMappings: FieldMappingForm[];
        sort?: string;
        filterCond?: string;
    };
}

export interface ModifiedAtFieldForm extends FieldForm {}

export interface ModifierFieldForm extends FieldForm {}

export interface MultipleCheckFieldForm extends FieldForm {
    defaultValue?: string[];
    options: {
        [key: string]: OptionForm;
    };
    align?: 'HORIZONTAL' | 'VERTICAL';
}

export interface MultipleLineTextFieldForm extends FieldForm {
    defaultValue?: string;
}

export interface MultipleSelectFieldForm extends FieldForm {
    defaultValue?: string[];
    options: {
        [key: string]: OptionForm;
    };
}


export interface OrganizationSelectFieldForm extends FieldForm {
    entities: EntityForm[];
    defaultValue?: EntityForm[];
}

export interface RecordIdFieldForm extends FieldForm {}

export interface ReferenceTableFieldForm extends FieldForm {
    referenceTable: {
        condition: {
            relatedField: string;
            field: string;
        };
        size?: string;
        relatedApp: {
            app: string;
            code: string;
        };
        sort?: string;
        filterCond?: string;
        displayFields: string[];
    };
}

export interface SingleCheckFieldForm extends FieldForm {
    defaultValue?: string;
    options: {
        [key: string]: OptionForm;
    };
    align?: 'HORIZONTAL' | 'VERTICAL';
}

export interface SingleLineTextFieldForm extends FieldForm {
    expression?: string;
    defaultValue?: string;
    minLength?: string;
    unique?: boolean;
    maxLength?: string;
    hideExpression?: boolean;
}

export interface SingleSelectFieldForm extends FieldForm {
    defaultValue?: string;
    options: {
        [key: string]: OptionForm;
    };
}

export interface StatusAssigneeFieldForm extends FieldForm {
    enabled?: boolean;
}

export interface StatusFieldForm extends FieldForm {
    enabled?: boolean;
}

export interface TableForm extends FieldForm {
    fields: {
        [key: string]: CalcFieldForm | CategoryFieldForm | DateFieldForm | DatetimeFieldForm | DecimalFieldForm | EditorFieldForm | FileFieldForm | GroupFieldForm | GroupSelectFieldForm | LinkFieldForm | LookupFieldForm | ModifiedAtFieldForm | ModifierFieldForm | MultipleCheckFieldForm | MultipleLineTextFieldForm | MultipleSelectFieldForm | OrganizationSelectFieldForm | RecordIdFieldForm | ReferenceTableFieldForm | SingleCheckFieldForm | SingleLineTextFieldForm | SingleSelectFieldForm | StatusAssigneeFieldForm | StatusFieldForm | TableForm | TimeFieldForm | UserSelectFieldForm;
    };
}

export interface TimeFieldForm extends FieldForm {
    defaultValue?: string;
    defaultNowValue?: boolean;
}

export interface UserSelectFieldForm extends FieldForm {
    entities: EntityForm[];
    defaultValue?: EntityForm[];
}