import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type { BelongsToGetAssociationMixin } from 'sequelize';
import type { Classes } from './classes';

/**
 * Class-level daily attendance aggregate (WS-C), filled by the class teacher
 * (same shape as the campus aggregate). It is the **source** that rolls up to
 * campus → school → org: the owning class's `campusId`/`schoolId`/
 * `organizationId` are denormalized onto the row so a tier rollup is a simple
 * `SUM` with a single-column filter (no joins). One row per class per date.
 */
export class ClassAttendance extends Model<
  InferAttributes<ClassAttendance>,
  InferCreationAttributes<ClassAttendance>
> {
  declare id: CreationOptional<string>;
  declare classId: 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: CreationOptional<string | null>;
  declare recorded_by_label: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare schoolId: CreationOptional<string | null>;
  declare campusId: 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 getClass: BelongsToGetAssociationMixin<Classes>;

  static associate(db: Db): void {
    db.class_attendance.belongsTo(db.classes, {
      as: 'class',
      foreignKey: { name: 'classId' },
      constraints: false,
    });
  }
}

export default function (sequelize: Sequelize): typeof ClassAttendance {
  ClassAttendance.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      classId: { type: DataTypes.UUID, 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: true },
      recorded_by_label: { type: DataTypes.TEXT, allowNull: true },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      schoolId: { type: DataTypes.UUID, allowNull: true },
      campusId: { 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: 'class_attendance',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return ClassAttendance;
}
