import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../config/database';
import { User } from './User';

interface WorkflowTemplateAttributes {
    templateId: string;
    templateName: string;
    templateCode?: string;
    templateDescription?: string;
    templateCategory?: string;
    workflowType?: string;
    approvalLevelsConfig?: any;
    defaultTatHours?: number;
    formStepsConfig?: any;
    userFieldMappings?: any;
    dynamicApproverConfig?: any;
    isActive: boolean;
    isSystemTemplate: boolean;
    usageCount: number;
    createdBy?: string;
    createdAt: Date;
    updatedAt: Date;
}

interface WorkflowTemplateCreationAttributes extends Optional<WorkflowTemplateAttributes, 'templateId' | 'templateCode' | 'templateDescription' | 'templateCategory' | 'workflowType' | 'approvalLevelsConfig' | 'defaultTatHours' | 'formStepsConfig' | 'userFieldMappings' | 'dynamicApproverConfig' | 'createdBy' | 'createdAt' | 'updatedAt'> { }

export class WorkflowTemplate extends Model<WorkflowTemplateAttributes, WorkflowTemplateCreationAttributes> implements WorkflowTemplateAttributes {
    public templateId!: string;
    public templateName!: string;
    public templateCode?: string;
    public templateDescription?: string;
    public templateCategory?: string;
    public workflowType?: string;
    public approvalLevelsConfig?: any;
    public defaultTatHours?: number;
    public formStepsConfig?: any;
    public userFieldMappings?: any;
    public dynamicApproverConfig?: any;
    public isActive!: boolean;
    public isSystemTemplate!: boolean;
    public usageCount!: number;
    public createdBy?: string;
    public createdAt!: Date;
    public updatedAt!: Date;

    // Associations
    public creator?: User;
}

WorkflowTemplate.init(
    {
        templateId: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
            field: 'template_id'
        },
        templateName: {
            type: DataTypes.STRING(200),
            allowNull: false,
            field: 'template_name'
        },
        templateCode: {
            type: DataTypes.STRING(50),
            allowNull: true,
            unique: true,
            field: 'template_code'
        },
        templateDescription: {
            type: DataTypes.TEXT,
            allowNull: true,
            field: 'template_description'
        },
        templateCategory: {
            type: DataTypes.STRING(100),
            allowNull: true,
            field: 'template_category'
        },
        workflowType: {
            type: DataTypes.STRING(50),
            allowNull: true,
            field: 'workflow_type'
        },
        approvalLevelsConfig: {
            type: DataTypes.JSONB,
            allowNull: true,
            field: 'approval_levels_config'
        },
        defaultTatHours: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true,
            defaultValue: 24,
            field: 'default_tat_hours'
        },
        formStepsConfig: {
            type: DataTypes.JSONB,
            allowNull: true,
            field: 'form_steps_config'
        },
        userFieldMappings: {
            type: DataTypes.JSONB,
            allowNull: true,
            field: 'user_field_mappings'
        },
        dynamicApproverConfig: {
            type: DataTypes.JSONB,
            allowNull: true,
            field: 'dynamic_approver_config'
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true,
            field: 'is_active'
        },
        isSystemTemplate: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: false,
            field: 'is_system_template'
        },
        usageCount: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: 0,
            field: 'usage_count'
        },
        createdBy: {
            type: DataTypes.UUID,
            allowNull: true,
            field: 'created_by',
            references: {
                model: 'users',
                key: 'user_id'
            }
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
            field: 'created_at'
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
            field: 'updated_at'
        }
    },
    {
        sequelize,
        modelName: 'WorkflowTemplate',
        tableName: 'workflow_templates',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        indexes: [
            {
                unique: true,
                fields: ['template_code']
            },
            {
                fields: ['workflow_type']
            },
            {
                fields: ['is_active']
            }
        ]
    }
);

// Associations
WorkflowTemplate.belongsTo(User, {
    as: 'creator',
    foreignKey: 'createdBy',
    targetKey: 'userId'
});
