/*
  # Create servers table

  1. New Tables
    - `servers`
      - `id` (uuid, primary key)
      - `name` (text)
      - `model` (text)
      - `cpus` (jsonb array)
      - `ram_gb` (integer)
      - `proxmox_url` (text)
      - `user_id` (uuid, foreign key)
      - `created_at` (timestamp)
      - `status` (text)
      - `specs` (jsonb)
      - `last_ping` (integer)

  2. Security
    - Enable RLS on `servers` table
    - Add policies for authenticated users to manage their own servers
*/

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

ALTER TABLE servers ENABLE ROW LEVEL SECURITY;

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);