-- input_history 테이블
CREATE TABLE input_history (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_name VARCHAR(255) NOT NULL,
    region VARCHAR(100) NOT NULL,
    task_id CHAR(36) NOT NULL UNIQUE,  -- 유니크 UUID
    detail_region_info TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- upload_img_url 테이블
CREATE TABLE upload_img_url (
    id INT AUTO_INCREMENT PRIMARY KEY,
    task_id CHAR(36) NOT NULL,  -- input_history와 연결
    img_uid INT NOT NULL,
    img_url VARCHAR(2048) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_img_task_image (task_id, img_uid),
    FOREIGN KEY (task_id) REFERENCES input_history(task_id) ON DELETE CASCADE
);

-- lyrics 테이블
CREATE TABLE lyrics (
    id INT AUTO_INCREMENT PRIMARY KEY,
    input_history_id INT NOT NULL,
    task_id CHAR(36) NOT NULL UNIQUE,
    status VARCHAR(50) NOT NULL,
    lyrics_prompt TEXT NOT NULL,
    lyrics_result LONGTEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE
);

-- song 테이블
CREATE TABLE song (
    id INT AUTO_INCREMENT PRIMARY KEY,
    input_history_id INT NOT NULL,
    lyrics_id INT NOT NULL,
    task_id CHAR(36) NOT NULL UNIQUE,
    status VARCHAR(50) NOT NULL,
    song_prompt TEXT NOT NULL,
    song_result_url_1 VARCHAR(2048),
    song_result_url_2 VARCHAR(2048),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
    FOREIGN KEY (lyrics_id) REFERENCES lyrics(id) ON DELETE CASCADE
);

-- creatomate_result_url 테이블
CREATE TABLE creatomate_result_url (
    id INT AUTO_INCREMENT PRIMARY KEY,
    input_history_id INT NOT NULL,
    song_id INT NOT NULL,
    task_id CHAR(36) NOT NULL UNIQUE,
    status VARCHAR(50) NOT NULL,
    result_movie_url VARCHAR(2048),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (input_history_id) REFERENCES input_history(id) ON DELETE CASCADE,
    FOREIGN KEY (song_id) REFERENCES song(id) ON DELETE CASCADE
);

-- ===== 인덱스 추가 (쿼리 성능 최적화) =====

-- input_history
CREATE INDEX idx_input_history_task_id ON input_history(task_id);

-- upload_img_url (task_id 인덱스 + 복합 인덱스)
CREATE INDEX idx_upload_img_url_task_id ON upload_img_url(task_id);
CREATE INDEX idx_upload_img_url_task_id_img_uid ON upload_img_url(task_id, img_uid);

-- lyrics (input_history_id + task_id 인덱스)
CREATE INDEX idx_lyrics_input_history_id ON lyrics(input_history_id);
CREATE INDEX idx_lyrics_task_id ON lyrics(task_id);

-- song (input_history_id + lyrics_id + task_id 인덱스)
CREATE INDEX idx_song_input_history_id ON song(input_history_id);
CREATE INDEX idx_song_lyrics_id ON song(lyrics_id);
CREATE INDEX idx_song_task_id ON song(task_id);

-- creatomate_result_url (input_history_id + song_id + task_id 인덱스)
CREATE INDEX idx_creatomate_input_history_id ON creatomate_result_url(input_history_id);
CREATE INDEX idx_creatomate_song_id ON creatomate_result_url(song_id);
CREATE INDEX idx_creatomate_task_id ON creatomate_result_url(task_id);