/*
  # Create servers table for Proxmox dashboard

  1. New Tables
    - `servers`
      - `id` (uuid, primary key)
      - `name` (text)
      - `url` (text)
      - `status` (text)
      - `specs` (jsonb)
      - `last_ping` (integer)
      - `user_id` (uuid, foreign key to auth.users)
      - `created_at` (timestamp with time zone)

  2. Security
    - Enable RLS on `servers` table
    - Add policies for authenticated users to:
      - Read their own servers
      - Insert their own servers
      - Update their own servers
      - Delete their own servers
*/

CREATE TABLE IF NOT EXISTS servers (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  name text NOT NULL,
  url text NOT NULL,
  status text NOT NULL DEFAULT 'checking',
  specs jsonb NOT NULL DEFAULT '{}'::jsonb,
  last_ping integer,
  user_id uuid REFERENCES auth.users(id) NOT NULL,
  created_at timestamptz DEFAULT now() NOT NULL
);

-- Enable Row Level Security
ALTER TABLE servers ENABLE ROW LEVEL SECURITY;

-- Create policies
CREATE POLICY "Users can read their own servers"
  ON servers
  FOR SELECT
  TO authenticated
  USING (auth.uid() = user_id);

CREATE POLICY "Users can insert their own servers"
  ON servers
  FOR INSERT
  TO authenticated
  WITH CHECK (auth.uid() = user_id);

CREATE POLICY "Users can update their own servers"
  ON servers
  FOR UPDATE
  TO authenticated
  USING (auth.uid() = user_id)
  WITH CHECK (auth.uid() = user_id);

CREATE POLICY "Users can delete their own servers"
  ON servers
  FOR DELETE
  TO authenticated
  USING (auth.uid() = user_id);