-- Migration: Create job_alerts table
-- Description: Table for job search alerts sent to candidates via email

CREATE TABLE IF NOT EXISTS job_alerts (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    
    -- Alert configuration
    search_query VARCHAR(255),
    location VARCHAR(255),
    employment_type VARCHAR(50),
    work_mode VARCHAR(50),
    salary_min DECIMAL(12,2),
    salary_max DECIMAL(12,2),
    currency VARCHAR(3) DEFAULT 'BRL',
    
    -- Alert metadata
    frequency VARCHAR(20) NOT NULL DEFAULT 'daily', -- daily, weekly
    is_active BOOLEAN NOT NULL DEFAULT true,
    last_sent_at TIMESTAMP WITH TIME ZONE,
    next_send_at TIMESTAMP WITH TIME ZONE,
    
    -- Confirmation
    confirmation_token VARCHAR(255),
    confirmed_at TIMESTAMP WITH TIME ZONE,
    
    -- Metadata
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_job_alerts_user_id ON job_alerts(user_id);
CREATE INDEX IF NOT EXISTS idx_job_alerts_is_active ON job_alerts(is_active);
CREATE INDEX IF NOT EXISTS idx_job_alerts_next_send_at ON job_alerts(next_send_at);

-- Comments
COMMENT ON TABLE job_alerts IS 'Job search alerts for candidates';
COMMENT ON COLUMN job_alerts.user_id IS 'User who created the alert (NULL for guest alerts)';
COMMENT ON COLUMN job_alerts.frequency IS 'How often to send alerts: daily, weekly';
COMMENT ON COLUMN job_alerts.confirmation_token IS 'Token for email confirmation';
