-- Database initialization script for Docker container
-- This script sets up the initial database schema

-- Create sessions table for auth
CREATE TABLE IF NOT EXISTS sessions (
  sid VARCHAR PRIMARY KEY,
  sess JSONB NOT NULL,
  expire TIMESTAMP NOT NULL
);

-- Create index for sessions
CREATE INDEX IF NOT EXISTS IDX_session_expire ON sessions(expire);

-- Create enum for user roles
CREATE TYPE user_role AS ENUM ('dj', 'admin');

-- Create users table
CREATE TABLE IF NOT EXISTS users (
  id VARCHAR PRIMARY KEY,
  email VARCHAR UNIQUE,
  first_name VARCHAR,
  last_name VARCHAR,
  profile_image_url VARCHAR,
  display_name VARCHAR,
  bio TEXT,
  phone VARCHAR,
  location VARCHAR,
  website VARCHAR,
  role user_role DEFAULT 'dj',
  is_active BOOLEAN DEFAULT true,
  max_monthly_events INTEGER DEFAULT 4,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create event types table
CREATE TABLE IF NOT EXISTS event_types (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  description TEXT,
  color VARCHAR DEFAULT '#3B82F6',
  is_active BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create schedule templates table
CREATE TABLE IF NOT EXISTS schedule_templates (
  id SERIAL PRIMARY KEY,
  name VARCHAR NOT NULL,
  description TEXT,
  is_active BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create schedule template slots table
CREATE TABLE IF NOT EXISTS schedule_template_slots (
  id SERIAL PRIMARY KEY,
  template_id INTEGER REFERENCES schedule_templates(id) ON DELETE CASCADE,
  day_of_week INTEGER NOT NULL CHECK (day_of_week >= 0 AND day_of_week <= 6),
  start_time TIME NOT NULL,
  end_time TIME NOT NULL,
  event_type_id INTEGER REFERENCES event_types(id),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create events table
CREATE TABLE IF NOT EXISTS events (
  id SERIAL PRIMARY KEY,
  title VARCHAR NOT NULL,
  description TEXT,
  date DATE NOT NULL,
  start_time TIME NOT NULL,
  end_time TIME NOT NULL,
  location VARCHAR,
  event_type_id INTEGER REFERENCES event_types(id),
  dj_id VARCHAR REFERENCES users(id),
  is_admin_assigned BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create social links table
CREATE TABLE IF NOT EXISTS social_links (
  id SERIAL PRIMARY KEY,
  dj_id VARCHAR REFERENCES users(id) ON DELETE CASCADE,
  platform VARCHAR NOT NULL,
  url VARCHAR NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create availability table
CREATE TABLE IF NOT EXISTS availability (
  id SERIAL PRIMARY KEY,
  dj_id VARCHAR REFERENCES users(id) ON DELETE CASCADE,
  date DATE NOT NULL,
  is_available BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE(dj_id, date)
);

-- Create slot eligibility table
CREATE TABLE IF NOT EXISTS slot_eligibility (
  id SERIAL PRIMARY KEY,
  dj_id VARCHAR REFERENCES users(id) ON DELETE CASCADE,
  event_type_id INTEGER REFERENCES event_types(id),
  is_eligible BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create removal requests table
CREATE TABLE IF NOT EXISTS removal_requests (
  id SERIAL PRIMARY KEY,
  event_id INTEGER REFERENCES events(id) ON DELETE CASCADE,
  dj_id VARCHAR REFERENCES users(id),
  reason TEXT NOT NULL,
  status VARCHAR DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'denied')),
  reviewed_by VARCHAR REFERENCES users(id),
  reviewed_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create invitations table
CREATE TABLE IF NOT EXISTS invitations (
  id SERIAL PRIMARY KEY,
  email VARCHAR NOT NULL,
  token VARCHAR UNIQUE NOT NULL,
  expires_at TIMESTAMP NOT NULL,
  is_used BOOLEAN DEFAULT false,
  created_by VARCHAR REFERENCES users(id),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Insert some default event types
INSERT INTO event_types (name, description, color) VALUES
  ('Radio Show', 'Regular radio broadcasting', '#3B82F6'),
  ('Club Night', 'Club and nightclub events', '#10B981'),
  ('Wedding', 'Wedding and celebration events', '#F59E0B'),
  ('Corporate Event', 'Business and corporate functions', '#6366F1'),
  ('Festival', 'Music festivals and outdoor events', '#EF4444'),
  ('Special Event', 'Special occasions and one-time events', '#8B5CF6')
ON CONFLICT DO NOTHING;