import { char, int, mysqlEnum, mysqlTable } from 'drizzle-orm/mysql-core';
import { report } from '@db/schema/report.ts';
import type { MySql2Database } from 'drizzle-orm/mysql2';
import { eq } from 'drizzle-orm';

type Database = MySql2Database<{ reportAttachment: typeof reportAttachment }>;

export const reportAttachment = mysqlTable('report_attachment', {
  type: mysqlEnum('type', ['image', 'video']),
  hash: char('hash', { length: 32 }),
  reportId: int('report_id')
    .notNull()
    .references(() => report.id)
});

export type AddReportAttachmentReq = {
  type: 'image' | 'video';
  hash: string;
  reportId: number;
};

export type GetReportAttachmentsReq = {
  reportId: number;
};

export async function addReportAttachment(db: Database, values: AddReportAttachmentReq) {
  await db.insert(reportAttachment).values(values);
}

export async function getReportAttachments(db: Database, values: GetReportAttachmentsReq) {
  return db
    .select({
      type: reportAttachment.type,
      hash: reportAttachment.hash
    })
    .from(reportAttachment)
    .where(eq(reportAttachment.reportId, values.reportId));
}
