import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
} from 'sequelize';
import type { Campuses } from './campuses';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class CampusAttendanceSummaries extends Model<
  InferAttributes<CampusAttendanceSummaries>,
  InferCreationAttributes<CampusAttendanceSummaries>
> {
  declare id: CreationOptional<string>;
  declare campus_key: string;
  declare attendance_date: string;
  declare total_enrolled: number;
  declare total_present: number;
  declare total_absent: number;
  declare total_tardy: CreationOptional<number>;
  declare attendance_percentage: string;
  declare recorded_by_label: string | null;
  declare notes: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string>;
  declare campusId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, 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.campus_attendance_summaries.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.campus_attendance_summaries.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: {
        name: 'campusId',
      },
      constraints: false,
    });

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

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

export default function (sequelize: Sequelize): typeof CampusAttendanceSummaries {
  CampusAttendanceSummaries.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      campus_key: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      attendance_date: {
        type: DataTypes.DATEONLY,
        allowNull: false,
      },
      total_enrolled: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      total_present: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      total_absent: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      total_tardy: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      attendance_percentage: {
        type: DataTypes.DECIMAL(5, 2),
        allowNull: false,
      },
      recorded_by_label: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      notes: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
      organizationId: { type: DataTypes.UUID, allowNull: false },
      campusId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: false },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'campus_attendance_summaries',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return CampusAttendanceSummaries;
}
