/*
  # Initial Database Schema for Zouis Corp

  1. New Tables
    - `jobs`
      - `id` (uuid, primary key)
      - `title` (text)
      - `type` (text) - Full-time, Part-time, Contract, Internship
      - `location` (text)
      - `salary` (text)
      - `category` (text) - development, design, marketing
      - `experience` (text)
      - `description` (text)
      - `requirements` (text array)
      - `is_active` (boolean)
      - `created_at` (timestamp)
      - `updated_at` (timestamp)

    - `job_applications`
      - `id` (uuid, primary key)
      - `job_id` (uuid, foreign key)
      - `name` (text)
      - `email` (text)
      - `phone` (text)
      - `resume_url` (text)
      - `cover_letter` (text)
      - `status` (text) - pending, reviewing, interviewed, hired, rejected
      - `created_at` (timestamp)

    - `certificates`
      - `id` (uuid, primary key)
      - `certificate_id` (text, unique)
      - `student_name` (text)
      - `course_name` (text)
      - `issue_date` (date)
      - `completion_date` (date)
      - `grade` (text)
      - `instructor` (text)
      - `is_valid` (boolean)
      - `created_at` (timestamp)

    - `contact_messages`
      - `id` (uuid, primary key)
      - `name` (text)
      - `email` (text)
      - `phone` (text)
      - `service` (text)
      - `message` (text)
      - `status` (text) - new, read, responded, closed
      - `created_at` (timestamp)

    - `portfolio_projects`
      - `id` (uuid, primary key)
      - `title` (text)
      - `description` (text)
      - `image_url` (text)
      - `tech_stack` (text array)
      - `category` (text)
      - `project_url` (text)
      - `github_url` (text)
      - `is_featured` (boolean)
      - `created_at` (timestamp)

    - `testimonials`
      - `id` (uuid, primary key)
      - `client_name` (text)
      - `client_role` (text)
      - `client_company` (text)
      - `client_image` (text)
      - `content` (text)
      - `rating` (integer)
      - `is_featured` (boolean)
      - `created_at` (timestamp)

  2. Security
    - Enable RLS on all tables
    - Add policies for public read access where appropriate
    - Add policies for authenticated admin access for management
*/

-- Jobs table
CREATE TABLE IF NOT EXISTS jobs (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  title text NOT NULL,
  type text NOT NULL CHECK (type IN ('Full-time', 'Part-time', 'Contract', 'Internship')),
  location text NOT NULL,
  salary text NOT NULL,
  category text NOT NULL CHECK (category IN ('development', 'design', 'marketing', 'other')),
  experience text NOT NULL,
  description text NOT NULL,
  requirements text[] NOT NULL DEFAULT '{}',
  is_active boolean DEFAULT true,
  created_at timestamptz DEFAULT now(),
  updated_at timestamptz DEFAULT now()
);

-- Job applications table
CREATE TABLE IF NOT EXISTS job_applications (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  job_id uuid REFERENCES jobs(id) ON DELETE CASCADE,
  name text NOT NULL,
  email text NOT NULL,
  phone text,
  resume_url text,
  cover_letter text,
  status text DEFAULT 'pending' CHECK (status IN ('pending', 'reviewing', 'interviewed', 'hired', 'rejected')),
  created_at timestamptz DEFAULT now()
);

-- Certificates table
CREATE TABLE IF NOT EXISTS certificates (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  certificate_id text UNIQUE NOT NULL,
  student_name text NOT NULL,
  course_name text NOT NULL,
  issue_date date NOT NULL,
  completion_date date NOT NULL,
  grade text DEFAULT 'Pass',
  instructor text NOT NULL,
  is_valid boolean DEFAULT true,
  created_at timestamptz DEFAULT now()
);

-- Contact messages table
CREATE TABLE IF NOT EXISTS contact_messages (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  name text NOT NULL,
  email text NOT NULL,
  phone text,
  service text,
  message text NOT NULL,
  status text DEFAULT 'new' CHECK (status IN ('new', 'read', 'responded', 'closed')),
  created_at timestamptz DEFAULT now()
);

-- Portfolio projects table
CREATE TABLE IF NOT EXISTS portfolio_projects (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  title text NOT NULL,
  description text NOT NULL,
  image_url text NOT NULL,
  tech_stack text[] NOT NULL DEFAULT '{}',
  category text NOT NULL,
  project_url text,
  github_url text,
  is_featured boolean DEFAULT false,
  created_at timestamptz DEFAULT now()
);

-- Testimonials table
CREATE TABLE IF NOT EXISTS testimonials (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  client_name text NOT NULL,
  client_role text NOT NULL,
  client_company text NOT NULL,
  client_image text NOT NULL,
  content text NOT NULL,
  rating integer DEFAULT 5 CHECK (rating >= 1 AND rating <= 5),
  is_featured boolean DEFAULT false,
  created_at timestamptz DEFAULT now()
);

-- Enable Row Level Security
ALTER TABLE jobs ENABLE ROW LEVEL SECURITY;
ALTER TABLE job_applications ENABLE ROW LEVEL SECURITY;
ALTER TABLE certificates ENABLE ROW LEVEL SECURITY;
ALTER TABLE contact_messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE portfolio_projects ENABLE ROW LEVEL SECURITY;
ALTER TABLE testimonials ENABLE ROW LEVEL SECURITY;

-- Public read policies for jobs
CREATE POLICY "Anyone can view active jobs"
  ON jobs
  FOR SELECT
  TO anon, authenticated
  USING (is_active = true);

-- Public read policies for portfolio projects
CREATE POLICY "Anyone can view portfolio projects"
  ON portfolio_projects
  FOR SELECT
  TO anon, authenticated
  USING (true);

-- Public read policies for testimonials
CREATE POLICY "Anyone can view testimonials"
  ON testimonials
  FOR SELECT
  TO anon, authenticated
  USING (true);

-- Public read policies for certificates (for verification)
CREATE POLICY "Anyone can verify certificates"
  ON certificates
  FOR SELECT
  TO anon, authenticated
  USING (is_valid = true);

-- Public insert policies for applications and contact
CREATE POLICY "Anyone can submit job applications"
  ON job_applications
  FOR INSERT
  TO anon, authenticated
  WITH CHECK (true);

CREATE POLICY "Anyone can submit contact messages"
  ON contact_messages
  FOR INSERT
  TO anon, authenticated
  WITH CHECK (true);

-- Admin policies (authenticated users can manage all data)
CREATE POLICY "Authenticated users can manage jobs"
  ON jobs
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);

CREATE POLICY "Authenticated users can view applications"
  ON job_applications
  FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "Authenticated users can manage certificates"
  ON certificates
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);

CREATE POLICY "Authenticated users can view contact messages"
  ON contact_messages
  FOR SELECT
  TO authenticated
  USING (true);

CREATE POLICY "Authenticated users can manage portfolio"
  ON portfolio_projects
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);

CREATE POLICY "Authenticated users can manage testimonials"
  ON testimonials
  FOR ALL
  TO authenticated
  USING (true)
  WITH CHECK (true);