-- Enable necessary extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Create organizations table
CREATE TABLE organizations (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  name TEXT NOT NULL,
  logo TEXT,
  stripe_account_id TEXT,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create users table with organization reference
CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  email TEXT UNIQUE NOT NULL,
  name TEXT,
  organization_id UUID REFERENCES organizations(id),
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create events table
CREATE TABLE events (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  title TEXT NOT NULL,
  slug TEXT NOT NULL,
  venue TEXT NOT NULL,
  start_time TIMESTAMP WITH TIME ZONE NOT NULL,
  description TEXT,
  created_by UUID REFERENCES users(id) NOT NULL,
  organization_id UUID REFERENCES organizations(id) NOT NULL,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(slug, organization_id)
);

-- Create tickets table
CREATE TABLE tickets (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  event_id UUID REFERENCES events(id) NOT NULL,
  uuid TEXT UNIQUE NOT NULL DEFAULT uuid_generate_v4()::TEXT,
  price DECIMAL(10,2) NOT NULL,
  purchaser_email TEXT NOT NULL,
  purchaser_name TEXT,
  checked_in BOOLEAN DEFAULT FALSE,
  scanned_at TIMESTAMP WITH TIME ZONE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create payouts table
CREATE TABLE payouts (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  event_id UUID REFERENCES events(id) NOT NULL,
  gross DECIMAL(10,2) NOT NULL,
  fee DECIMAL(10,2) NOT NULL,
  net DECIMAL(10,2) NOT NULL,
  stripe_transfer_id TEXT,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Enable Row Level Security
ALTER TABLE organizations ENABLE ROW LEVEL SECURITY;
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE events ENABLE ROW LEVEL SECURITY;
ALTER TABLE tickets ENABLE ROW LEVEL SECURITY;
ALTER TABLE payouts ENABLE ROW LEVEL SECURITY;

-- RLS Policies for organizations
CREATE POLICY "Users can view their own organization" ON organizations
  FOR SELECT USING (id IN (
    SELECT organization_id FROM users WHERE id = auth.uid()
  ));

CREATE POLICY "Users can update their own organization" ON organizations
  FOR UPDATE USING (id IN (
    SELECT organization_id FROM users WHERE id = auth.uid()
  ));

-- RLS Policies for users
CREATE POLICY "Users can view their own profile" ON users
  FOR SELECT USING (id = auth.uid());

CREATE POLICY "Users can update their own profile" ON users
  FOR UPDATE USING (id = auth.uid());

-- RLS Policies for events
CREATE POLICY "Users can view events from their organization" ON events
  FOR SELECT USING (organization_id IN (
    SELECT organization_id FROM users WHERE id = auth.uid()
  ));

CREATE POLICY "Users can create events for their organization" ON events
  FOR INSERT WITH CHECK (
    organization_id IN (
      SELECT organization_id FROM users WHERE id = auth.uid()
    ) AND created_by = auth.uid()
  );

CREATE POLICY "Users can update events they created" ON events
  FOR UPDATE USING (created_by = auth.uid());

CREATE POLICY "Users can delete events they created" ON events
  FOR DELETE USING (created_by = auth.uid());

-- RLS Policies for tickets
CREATE POLICY "Users can view tickets for their organization's events" ON tickets
  FOR SELECT USING (event_id IN (
    SELECT id FROM events WHERE organization_id IN (
      SELECT organization_id FROM users WHERE id = auth.uid()
    )
  ));

CREATE POLICY "Anyone can create tickets" ON tickets
  FOR INSERT WITH CHECK (true);

CREATE POLICY "Users can update tickets for their organization's events" ON tickets
  FOR UPDATE USING (event_id IN (
    SELECT id FROM events WHERE organization_id IN (
      SELECT organization_id FROM users WHERE id = auth.uid()
    )
  ));

-- RLS Policies for payouts
CREATE POLICY "Users can view payouts for their organization's events" ON payouts
  FOR SELECT USING (event_id IN (
    SELECT id FROM events WHERE organization_id IN (
      SELECT organization_id FROM users WHERE id = auth.uid()
    )
  ));

CREATE POLICY "Users can create payouts for their organization's events" ON payouts
  FOR INSERT WITH CHECK (event_id IN (
    SELECT id FROM events WHERE organization_id IN (
      SELECT organization_id FROM users WHERE id = auth.uid()
    )
  ));

-- Create indexes for better performance
CREATE INDEX idx_users_organization_id ON users(organization_id);
CREATE INDEX idx_events_organization_id ON events(organization_id);
CREATE INDEX idx_events_created_by ON events(created_by);
CREATE INDEX idx_events_slug ON events(slug);
CREATE INDEX idx_tickets_event_id ON tickets(event_id);
CREATE INDEX idx_tickets_uuid ON tickets(uuid);
CREATE INDEX idx_tickets_purchaser_email ON tickets(purchaser_email);
CREATE INDEX idx_payouts_event_id ON payouts(event_id);