-- Create Group table
CREATE TABLE UserGroup (
    id UUID PRIMARY KEY DEFAULT uuid(),
    group_name VARCHAR UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create User Table
CREATE TABLE User (
    id UUID PRIMARY KEY DEFAULT uuid(),
    name VARCHAR,
    username VARCHAR UNIQUE NOT NULL,
    email VARCHAR UNIQUE,
    group_id UUID,
    hashed_password VARCHAR NOT NULL,
    is_active BOOLEAN DEFAULT true,
    is_admin BOOLEAN DEFAULT false,
    last_login TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_at timestamp DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (group_id) REFERENCES UserGroup(id)
);

-- Create Contest Table
CREATE TABLE Contest (
    id UUID PRIMARY KEY DEFAULT uuid(),
    city_name VARCHAR NOT NULL,
    date DATE NOT NULL,
    round VARCHAR NOT NULL,
    is_active BOOLEAN DEFAULT false,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create Song Table with ARRAY type for tags
CREATE TABLE Song (
    id UUID PRIMARY KEY DEFAULT uuid(),
    contest_id UUID NOT NULL,
    artist_name VARCHAR NOT NULL,
    song_title VARCHAR NOT NULL,
    country VARCHAR NOT NULL,
    lyrics TEXT,
    song_meaning TEXT,
    tags VARCHAR[],
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (contest_id) REFERENCES Contest(id)
);

-- Create Reviews Table with Score Constraints
CREATE TABLE Reviews (
    id UUID PRIMARY KEY DEFAULT uuid(),
    song_id UUID NOT NULL,
    reviewer_id UUID NOT NULL,
    song_score INTEGER CHECK (song_score BETWEEN 1 AND 100),
    wardrobe_score INTEGER CHECK (wardrobe_score BETWEEN 1 AND 100),
    stage_show_score INTEGER CHECK (stage_show_score BETWEEN 1 AND 100),
    review_text TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (song_id) REFERENCES Song(id),
    FOREIGN KEY (reviewer_id) REFERENCES User(id)
);