CREATE DATABASE IF NOT EXISTS jukebox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE jukebox;

DROP TABLE IF EXISTS track_genre;
DROP TABLE IF EXISTS artist_track;
DROP TABLE IF EXISTS likes;
DROP TABLE IF EXISTS tracks;
DROP TABLE IF EXISTS artists;
DROP TABLE IF EXISTS albums;
DROP TABLE IF EXISTS genres;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS roles;
DROP TABLE IF EXISTS labels;

CREATE TABLE roles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name ENUM('admin', 'user') NOT NULL UNIQUE
) ENGINE=InnoDB;

CREATE TABLE labels (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE
) ENGINE=InnoDB;

CREATE TABLE genres (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE
) ENGINE=InnoDB;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    role_id INT NOT NULL,
    CONSTRAINT fk_users_role
        FOREIGN KEY (role_id) REFERENCES roles(id)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE albums (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    cover_path VARCHAR(255),
    release_date DATETIME,
    duration_seconds INT,
    type ENUM('single', 'ep', 'lp', 'album', 'compilation') NOT NULL DEFAULT 'album',
    label_id INT,
    CONSTRAINT fk_albums_label
        FOREIGN KEY (label_id) REFERENCES labels(id)
        ON UPDATE CASCADE
        ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE artists (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    cover_path VARCHAR(255),
    release_date DATETIME,
    label_id INT,
    duration INT,
    CONSTRAINT fk_artists_label
        FOREIGN KEY (label_id) REFERENCES labels(id)
        ON UPDATE CASCADE
        ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE tracks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    file_path VARCHAR(255) NOT NULL,
    title VARCHAR(255) NOT NULL,
    duration_seconds INT NOT NULL,
    album_id INT,
    CONSTRAINT fk_tracks_album
        FOREIGN KEY (album_id) REFERENCES albums(id)
        ON UPDATE CASCADE
        ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE likes (
    user_id INT NOT NULL,
    track_id INT NOT NULL,
    PRIMARY KEY (user_id, track_id),
    CONSTRAINT fk_likes_user
        FOREIGN KEY (user_id) REFERENCES users(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_likes_track
        FOREIGN KEY (track_id) REFERENCES tracks(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE artist_track (
    artist_id INT NOT NULL,
    track_id INT NOT NULL,
    PRIMARY KEY (artist_id, track_id),
    CONSTRAINT fk_artist_track_artist
        FOREIGN KEY (artist_id) REFERENCES artists(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_artist_track_track
        FOREIGN KEY (track_id) REFERENCES tracks(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE track_genre (
    track_id INT NOT NULL,
    genre_id INT NOT NULL,
    PRIMARY KEY (track_id, genre_id),
    CONSTRAINT fk_track_genre_track
        FOREIGN KEY (track_id) REFERENCES tracks(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_track_genre_genre
        FOREIGN KEY (genre_id) REFERENCES genres(id)
        ON UPDATE CASCADE
        ON DELETE CASCADE
) ENGINE=InnoDB;
