/*
  # Add page visibility system

  1. New Tables
    - `page_settings` for controlling page visibility

  2. Security
    - Enable RLS on page_settings table
    - Add policies for public read and authenticated write access
*/

-- Create page_settings table
CREATE TABLE IF NOT EXISTS page_settings (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  page_name text UNIQUE NOT NULL,
  page_title text NOT NULL,
  visible boolean DEFAULT true,
  display_order integer DEFAULT 0,
  created_at timestamptz DEFAULT now(),
  updated_at timestamptz DEFAULT now()
);

-- Enable RLS
ALTER TABLE page_settings ENABLE ROW LEVEL SECURITY;

-- Create policies
CREATE POLICY "Page settings are publicly readable"
  ON page_settings
  FOR SELECT
  TO public
  USING (true);

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

-- Add updated_at trigger
CREATE TRIGGER update_page_settings_updated_at
  BEFORE UPDATE ON page_settings
  FOR EACH ROW
  EXECUTE FUNCTION update_updated_at_column();

-- Insert default page settings
INSERT INTO page_settings (page_name, page_title, visible, display_order) VALUES
  ('about', 'About', true, 1),
  ('research', 'Research', true, 2),
  ('my-happiness', 'My Happiness', true, 3),
  ('blog', 'Blog', true, 4),
  ('contact', 'Contact', true, 5)
ON CONFLICT (page_name) DO NOTHING;