-- Location: supabase/migrations/20250126225029_taxi_module.sql
-- Schema Analysis: Existing transportation system with routes, stops, timetable
-- Integration Type: NEW_MODULE - Adding taxi directory functionality 
-- Dependencies: No direct references to existing tables (standalone module)

-- Create tables for taxi directory functionality
CREATE TABLE public.taxis (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name TEXT NOT NULL,
    phone TEXT NOT NULL,
    district TEXT NOT NULL,
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Create favorites table (conditional user relationship)
CREATE TABLE public.favorite_taxis (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL,
    taxi_id UUID REFERENCES public.taxis(id) ON DELETE CASCADE,
    created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Essential indexes for performance
CREATE INDEX idx_taxis_district ON public.taxis(district);
CREATE INDEX idx_taxis_is_active ON public.taxis(is_active);
CREATE INDEX idx_taxis_name ON public.taxis(name);
CREATE INDEX idx_favorite_taxis_user_id ON public.favorite_taxis(user_id);
CREATE INDEX idx_favorite_taxis_taxi_id ON public.favorite_taxis(taxi_id);

-- Unique constraint for user-taxi favorites
CREATE UNIQUE INDEX idx_favorite_taxis_unique ON public.favorite_taxis(user_id, taxi_id);

-- Enable RLS for security
ALTER TABLE public.taxis ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.favorite_taxis ENABLE ROW LEVEL SECURITY;

-- RLS Policies using Pattern 4: Public Read, Private Write
CREATE POLICY "public_can_read_taxis"
ON public.taxis
FOR SELECT
TO public
USING (true);

-- Pattern 2: Simple User Ownership for favorites
CREATE POLICY "users_manage_own_favorite_taxis"
ON public.favorite_taxis
FOR ALL
TO authenticated
USING (user_id = auth.uid())
WITH CHECK (user_id = auth.uid());

-- Allow anonymous users to read favorites (for local storage fallback)
CREATE POLICY "public_can_read_favorite_taxis"
ON public.favorite_taxis
FOR SELECT
TO public
USING (true);

-- Sample taxi data for different districts
DO $$
BEGIN
    INSERT INTO public.taxis (name, phone, district, is_active) VALUES
        ('Taxi Central Boquete', '+507 720-1234', 'Boquete', true),
        ('Taxi Flores David', '+507 775-5678', 'David', true),
        ('Taxi Montaña Verde', '+507 720-9101', 'Boquete', true),
        ('Taxi Ciudad David', '+507 775-1122', 'David', true),
        ('Taxi Volcán Express', '+507 771-3344', 'Volcán', true),
        ('Taxi Dolega Rápido', '+507 721-5566', 'Dolega', true),
        ('Taxi Bugaba Seguro', '+507 772-7788', 'Bugaba', true),
        ('Taxi Renacimiento', '+507 773-9900', 'Renacimiento', true),
        ('Taxi Alanje Centro', '+507 774-2233', 'Alanje', true),
        ('Taxi Boquerón', '+507 775-4455', 'Boquerón', true),
        ('Taxi Los Naranjos', '+507 720-6677', 'Boquete', true),
        ('Taxi San Lorenzo', '+507 775-8899', 'David', true);
END $$;