-- Database initialization for MerchantsOfHope Recruiting Platform

-- Create extension for UUID if not exists
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Create tenants table
CREATE TABLE IF NOT EXISTS tenants (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name VARCHAR(255) NOT NULL,
    subdomain VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create users table
CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID REFERENCES tenants(id),
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255),
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    role VARCHAR(50) DEFAULT 'job_seeker', -- job_seeker, job_provider, admin
    provider VARCHAR(50), -- google, facebook, oidc, local
    provider_id VARCHAR(255),
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create job_positions table
CREATE TABLE IF NOT EXISTS job_positions (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID REFERENCES tenants(id),
    title VARCHAR(255) NOT NULL,
    description TEXT,
    location VARCHAR(255),
    employment_type VARCHAR(50), -- full_time, part_time, contract, internship
    salary_min DECIMAL(10,2),
    salary_max DECIMAL(10,2),
    posted_by UUID REFERENCES users(id),
    status VARCHAR(50) DEFAULT 'draft', -- draft, published, closed
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create applications table
CREATE TABLE IF NOT EXISTS applications (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    job_position_id UUID REFERENCES job_positions(id),
    applicant_id UUID REFERENCES users(id),
    resume_path VARCHAR(500),
    cover_letter TEXT,
    status VARCHAR(50) DEFAULT 'submitted', -- submitted, under_review, accepted, rejected
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_job_positions_tenant_id ON job_positions(tenant_id);
CREATE INDEX IF NOT EXISTS idx_job_positions_status ON job_positions(status);
CREATE INDEX IF NOT EXISTS idx_applications_job_position_id ON applications(job_position_id);
CREATE INDEX IF NOT EXISTS idx_applications_applicant_id ON applications(applicant_id);

-- Insert a default tenant for testing
INSERT INTO tenants (name, subdomain) VALUES ('TSYS Group', 'tsys') ON CONFLICT (subdomain) DO NOTHING;

-- Insert a default admin user for testing
INSERT INTO users (tenant_id, email, password_hash, first_name, last_name, role) 
SELECT 
    (SELECT id FROM tenants WHERE subdomain = 'tsys'),
    'admin@merchantsOfHope.org',
    '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', -- 'password'
    'Admin',
    'User',
    'admin'
ON CONFLICT (email) DO NOTHING;