import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManySetAssociationsMixin,
} from 'sequelize';
import type { AttendanceSessions } from './attendance_sessions';
import type { Classes } from './classes';
import type { Messages } from './messages';
import type { Organizations } from './organizations';
import type { Staff } from './staff';
import type { Timetables } from './timetables';
import type { Users } from './users';

export class Campuses extends Model<
  InferAttributes<Campuses>,
  InferCreationAttributes<Campuses>
> {
  declare id: CreationOptional<string>;
  declare name: string;
  declare code: string;
  declare address: string | null;
  declare phone: string | null;
  declare email: string | null;
  declare mascot: string | null;
  declare color: string | null;
  declare bgGradient: string | null;
  declare borderColor: string | null;
  declare textColor: string | null;
  declare bgLight: string | null;
  declare description: string | null;
  declare isOnline: CreationOptional<boolean>;
  declare active: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;


  declare getStaff_campus: HasManyGetAssociationsMixin<Staff>;
  declare setStaff_campus: HasManySetAssociationsMixin<Staff, string>;
  declare getClasses_campus: HasManyGetAssociationsMixin<Classes>;
  declare setClasses_campus: HasManySetAssociationsMixin<Classes, string>;
  declare getTimetables_campus: HasManyGetAssociationsMixin<Timetables>;
  declare setTimetables_campus: HasManySetAssociationsMixin<Timetables, string>;
  declare getAttendance_sessions_campus: HasManyGetAssociationsMixin<AttendanceSessions>;
  declare setAttendance_sessions_campus: HasManySetAssociationsMixin<AttendanceSessions, string>;
  declare getMessages_campus: HasManyGetAssociationsMixin<Messages>;
  declare setMessages_campus: HasManySetAssociationsMixin<Messages, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {
    db.campuses.hasMany(db.staff, {
      as: 'staff_campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });

    db.campuses.hasMany(db.classes, {
      as: 'classes_campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });

    db.campuses.hasMany(db.timetables, {
      as: 'timetables_campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });

    db.campuses.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });

    db.campuses.hasMany(db.messages, {
      as: 'messages_campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });

    db.campuses.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: { name: 'organizationId' },
      constraints: false,
    });

    db.campuses.belongsTo(db.users, { as: 'createdBy' });
    db.campuses.belongsTo(db.users, { as: 'updatedBy' });
  }
}

export default function (sequelize: Sequelize): typeof Campuses {
  Campuses.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      name: { type: DataTypes.TEXT, allowNull: false },
      code: { type: DataTypes.TEXT, allowNull: false },
      address: { type: DataTypes.TEXT },
      phone: { type: DataTypes.TEXT },
      email: { type: DataTypes.TEXT },
      mascot: { type: DataTypes.TEXT },
      color: { type: DataTypes.TEXT },
      bgGradient: { type: DataTypes.TEXT },
      borderColor: { type: DataTypes.TEXT },
      textColor: { type: DataTypes.TEXT },
      bgLight: { type: DataTypes.TEXT },
      description: { type: DataTypes.TEXT },
      isOnline: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
    },
    {
      sequelize,
      modelName: 'campuses',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Campuses;
}
