CREATE TABLE organizations (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL,
plan text NOT NULL DEFAULT 'free',
max_leads integer NOT NULL DEFAULT 100,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE leads (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
name text NOT NULL,
email text,
phone text,
source text NOT NULL DEFAULT 'web',
status text NOT NULL DEFAULT 'new',
score integer NOT NULL DEFAULT 0 CHECK (score BETWEEN 0 AND 100),
assigned_to uuid REFERENCES auth.users(id),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE deals (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
org_id uuid NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
lead_id uuid NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
title text NOT NULL,
stage text NOT NULL DEFAULT 'prospect',
value numeric(10,2),
probability integer DEFAULT 0 CHECK (probability BETWEEN 0 AND 100),
closed_at date,
created_at timestamptz NOT NULL DEFAULT now()
);