import { query } from '../mysql';

export interface Video {
  id: number;
  created_at: Date;
  modified_at: Date;
  genre: string;
  sub_genre: string;
  scene: string;
  action: string;
  camera: string;
  image_prompt: string;
  video_prompt: string;
  image_path: string;
  video_path: string;
}

export class VideoModel {
  static async create(video: Omit<Video, 'id' | 'created_at' | 'modified_at'>): Promise<number> {
    const sql = 'INSERT INTO video (genre, sub_genre, scene, action, camera, image_prompt, video_prompt, image_path, video_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
    const params = [video.genre, video.sub_genre, video.scene, video.action, video.camera, video.image_prompt, video.video_prompt, video.image_path, video.video_path];
    const result: any = await query(sql, params);
    return result.insertId;
  }

  static async getById(id: number): Promise<Video | null> {
    const sql = 'SELECT * FROM video WHERE id = ?';
    const params = [id];
    const rows: any = await query(sql, params);
    return rows.length > 0 ? rows[0] as Video : null;
  }

  static async getAll(): Promise<Video[]> {
    const sql = 'SELECT * FROM video';
    const rows: any = await query(sql);
    return rows as Video[];
  }

  static async update(id: number, video: Partial<Omit<Video, 'id' | 'created_at' | 'modified_at'>>): Promise<boolean> {
    const fields = Object.keys(video);
    const values = Object.values(video);
    
    if (fields.length === 0) {
      return false;
    }

    const sql = `UPDATE video SET ${fields.map(field => `${field} = ?`).join(', ')} WHERE id = ?`;
    const params = [...values, id];
    const result: any = await query(sql, params);
    return result.affectedRows > 0;
  }

  static async delete(id: number): Promise<boolean> {
    const sql = 'DELETE FROM video WHERE id = ?';
    const params = [id];
    const result: any = await query(sql, params);
    return result.affectedRows > 0;
  }
}
