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 { Organizations } from './organizations';
import type { Users } from './users';

/**
 * Guardian ↔ student link (many-to-many). Students and guardians are **users**
 * (roles), not SIS entities, so this junction relates two `users` rows: a
 * student may have several guardians and a guardian several students.
 * `relationship` is an optional label (parent / grandparent / guardian …).
 * Tenant-scoped by `organizationId`; queries narrow further via the student's
 * class/campus.
 */
export class GuardianStudents extends Model<
  InferAttributes<GuardianStudents>,
  InferCreationAttributes<GuardianStudents>
> {
  declare id: CreationOptional<string>;
  declare guardianId: string;
  declare studentId: string;
  declare relationship: 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 getGuardian: BelongsToGetAssociationMixin<Users>;
  declare getStudent: BelongsToGetAssociationMixin<Users>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;

  static associate(db: Db): void {
    db.guardian_students.belongsTo(db.users, {
      as: 'guardian',
      foreignKey: { name: 'guardianId' },
      constraints: false,
    });

    db.guardian_students.belongsTo(db.users, {
      as: 'student',
      foreignKey: { name: 'studentId' },
      constraints: false,
    });

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

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

export default function (sequelize: Sequelize): typeof GuardianStudents {
  GuardianStudents.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      guardianId: { type: DataTypes.UUID, allowNull: false },
      studentId: { type: DataTypes.UUID, allowNull: false },
      relationship: { type: DataTypes.TEXT, allowNull: 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: 'guardian_students',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return GuardianStudents;
}
