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 { Organizations } from './organizations';
import type { Campuses } from './campuses';
import type { Users } from './users';
import {
  AUDIO_FILE_KINDS,
  type AudioFileKind,
} from '@/shared/constants/audio-files';

/** Synthesis recipe stored on `recipe` rows (interpreted client-side). */
export interface AudioRecipe {
  readonly voices: ReadonlyArray<{
    readonly waveform: string;
    readonly notes: ReadonlyArray<{
      readonly freq: number;
      readonly startAt: number;
      readonly duration: number;
      readonly gain: number;
      readonly attack?: number;
      readonly rampFreqTo?: number;
    }>;
  }>;
}

export class AudioFiles extends Model<
  InferAttributes<AudioFiles>,
  InferCreationAttributes<AudioFiles>
> {
  declare id: CreationOptional<string>;
  declare title: string;
  declare kind: CreationOptional<AudioFileKind>;
  declare url: CreationOptional<string | null>;
  declare recipe: CreationOptional<AudioRecipe | null>;
  declare is_default: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare organizationId: 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 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.audio_files.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: { name: 'organizationId' },
      constraints: false,
    });
    db.audio_files.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });
    db.audio_files.belongsTo(db.users, { as: 'createdBy' });
    db.audio_files.belongsTo(db.users, { as: 'updatedBy' });
  }
}

export default function (sequelize: Sequelize): typeof AudioFiles {
  AudioFiles.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      title: { type: DataTypes.TEXT, allowNull: false },
      kind: {
        type: DataTypes.ENUM(...AUDIO_FILE_KINDS),
        allowNull: false,
        defaultValue: 'file',
      },
      url: { type: DataTypes.STRING(2083), allowNull: true },
      recipe: { type: DataTypes.JSONB, allowNull: true },
      is_default: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      organizationId: { 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: 'audio_files',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return AudioFiles;
}
