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 WalkthroughCheckins extends Model<
  InferAttributes<WalkthroughCheckins>,
  InferCreationAttributes<WalkthroughCheckins>
> {
  declare id: CreationOptional<string>;
  declare teacher_name: string;
  declare classroom: string;
  declare director_name: string;
  declare check_in_date: string;
  declare check_in_time: string;
  declare attitude_rating: number;
  declare attitude_comment: string | null;
  declare classroom_management_rating: number;
  declare classroom_management_comment: string | null;
  declare cleanliness_rating: number;
  declare cleanliness_comment: string | null;
  declare vibes_rating: number;
  declare vibes_comment: string | null;
  declare team_dynamics_rating: number;
  declare team_dynamics_comment: string | null;
  declare emergency_exit_rating: number;
  declare emergency_exit_comment: string | null;
  declare lesson_plan_rating: number;
  declare lesson_plan_comment: string | null;
  declare overall_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.walkthrough_checkins.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

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

export default function (sequelize: Sequelize): typeof WalkthroughCheckins {
  WalkthroughCheckins.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      teacher_name: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      classroom: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      director_name: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      check_in_date: {
        type: DataTypes.DATEONLY,
        allowNull: false,
      },
      check_in_time: {
        type: DataTypes.TIME,
        allowNull: false,
      },
      attitude_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      attitude_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      classroom_management_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      classroom_management_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      cleanliness_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      cleanliness_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      vibes_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      vibes_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      team_dynamics_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      team_dynamics_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      emergency_exit_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      emergency_exit_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      lesson_plan_rating: {
        type: DataTypes.INTEGER,
        allowNull: false,
      },
      lesson_plan_comment: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      overall_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: 'walkthrough_checkins',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return WalkthroughCheckins;
}
