-- Migration: Create jobs table
-- Description: Job postings created by companies

CREATE TABLE IF NOT EXISTS jobs (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    company_id UUID NOT NULL,
    created_by UUID NOT NULL,  -- user who created the job
    
    -- Job Details
    title VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    
    -- Salary
    salary_min DECIMAL(12,2),
    salary_max DECIMAL(12,2),
    salary_type VARCHAR(20) CHECK (salary_type IN ('hourly', 'monthly', 'yearly')),
    
    -- Employment
    employment_type VARCHAR(30) CHECK (employment_type IN ('full-time', 'part-time', 'dispatch', 'contract')),
    working_hours VARCHAR(100),
    
    -- Location
    location VARCHAR(255),
    region_id INT,
    city_id INT,
    
    -- Requirements & Benefits (stored as JSON arrays)
    requirements JSONB,
    benefits JSONB,
    
    -- Visa & Language
    visa_support BOOLEAN DEFAULT false,
    language_level VARCHAR(20),  -- 'N5' | 'N4' | 'N3' | 'N2' | 'N1' | 'beginner' | 'none'
    
    -- Status
    status VARCHAR(20) DEFAULT 'open' CHECK (status IN ('open', 'closed', 'draft')),
    
    -- Metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    -- Foreign keys
    FOREIGN KEY (company_id) REFERENCES companies(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id),
    FOREIGN KEY (region_id) REFERENCES regions(id),
    FOREIGN KEY (city_id) REFERENCES cities(id)
);

-- Indexes for filtering and search
CREATE INDEX idx_jobs_company ON jobs(company_id);
CREATE INDEX idx_jobs_status ON jobs(status);
CREATE INDEX idx_jobs_region ON jobs(region_id);
CREATE INDEX idx_jobs_employment_type ON jobs(employment_type);
CREATE INDEX idx_jobs_visa_support ON jobs(visa_support);
CREATE INDEX idx_jobs_created_at ON jobs(created_at DESC);

-- Comments
COMMENT ON TABLE jobs IS 'Job postings created by companies';
COMMENT ON COLUMN jobs.visa_support IS 'Whether company provides visa sponsorship';
COMMENT ON COLUMN jobs.language_level IS 'Required Japanese language level (JLPT N5-N1, beginner, or none)';
COMMENT ON COLUMN jobs.requirements IS 'JSON array of job requirements';
COMMENT ON COLUMN jobs.benefits IS 'JSON array of job benefits';
