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

export interface TagVideo {
  id: number;
  created_at: Date;
  modified_at: Date;
  tag_id: number;
  video_id: number;
}

export class TagVideoModel {
  static async create(tagVideo: Omit<TagVideo, 'id' | 'created_at' | 'modified_at'>): Promise<number> {
    const sql = 'INSERT INTO tag_video (tag_id, video_id) VALUES (?, ?)';
    const params = [tagVideo.tag_id, tagVideo.video_id];
    const result: any = await query(sql, params);
    return result.insertId;
  }

  static async getByVideoId(videoId: number): Promise<TagVideo[]> {
    const sql = 'SELECT * FROM tag_video WHERE video_id = ?';
    const params = [videoId];
    const rows: any = await query(sql, params);
    return rows as TagVideo[];
  }

  static async getByTagId(tagId: number): Promise<TagVideo[]> {
    const sql = 'SELECT * FROM tag_video WHERE tag_id = ?';
    const params = [tagId];
    const rows: any = await query(sql, params);
    return rows as TagVideo[];
  }

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

  static async deleteByTagAndVideo(tagId: number, videoId: number): Promise<boolean> {
    const sql = 'DELETE FROM tag_video WHERE tag_id = ? AND video_id = ?';
    const params = [tagId, videoId];
    const result: any = await query(sql, params);
    return result.affectedRows > 0;
  }
}
