import { DataTypes, Model, Optional, Sequelize } from 'sequelize';

export interface CategoryApplicabilityAttributes extends CategoryApplicabilityInterface {}
export interface CategoryApplicabilityCreationAttributes extends Optional<CategoryApplicabilityAttributes, 'id'> {}

export class CategoryApplicability extends Model<CategoryApplicabilityAttributes, CategoryApplicabilityCreationAttributes> implements CategoryApplicabilityAttributes {
    public id!: number;
    public categoryId!: number;
    public applicableRanks!: number[] | null;
    public applicableShipTypes!: number[] | null;
    public applicableTo!: any | null;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof CategoryApplicability {
        CategoryApplicability.init({
            id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
            categoryId: { type: DataTypes.BIGINT, allowNull: false, comment: "category reference" },
            applicableRanks: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "array of rank IDs" },
            applicableShipTypes: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "array of ship type IDs" },
            applicableTo: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "json object for applicability (Seafarer, Ashore, Others)" }
        }, {
            sequelize,
            tableName: 'category_applicabilities',
            timestamps: true,
            underscored: true,
            indexes: [
                { name: 'category_applicability_category_index', fields: ['category_id'] }
            ]
        });
        return CategoryApplicability;
    }

    public static associate(models: any) {
        CategoryApplicability.belongsTo(models.Category, { foreignKey: 'categoryId', as: 'category' });
    }
}
