-- Create events table for calendar/planning functionality
CREATE TABLE IF NOT EXISTS events (
    id TEXT PRIMARY KEY DEFAULT generate_random_string(24),
    tablo_id TEXT NOT NULL,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    start_date DATE NOT NULL,
    start_time TIME NOT NULL,
    end_time TIME,
    created_by UUID NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
    
    -- Foreign key constraint to tablos table
    CONSTRAINT fk_events_tablo_id 
        FOREIGN KEY (tablo_id) REFERENCES tablos(id) ON DELETE CASCADE,
    
    -- Foreign key constraint to users table (auth.users)
    CONSTRAINT fk_events_created_by 
        FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE CASCADE
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_events_tablo_id ON events(tablo_id);
CREATE INDEX IF NOT EXISTS idx_events_created_by ON events(created_by);
CREATE INDEX IF NOT EXISTS idx_events_start_date ON events(start_date);
CREATE INDEX IF NOT EXISTS idx_events_deleted_at ON events(deleted_at);

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

-- Policy to allow users to view events from tablos they have access to
CREATE POLICY "Users can view events from accessible tablos" ON events
    FOR SELECT USING (
        EXISTS (
            SELECT 1 FROM user_tablos ut
            JOIN events ON ut.id = events.tablo_id
            WHERE events.deleted_at IS NULL
            AND (
                ut.user_id = (SELECT auth.uid())
            )
        )
    );

-- Policy to allow users to insert events into tablos they have access to
CREATE POLICY "Users can insert events into accessible tablos" ON events
    FOR INSERT WITH CHECK (
        (SELECT auth.uid()) = created_by
        AND EXISTS (
            SELECT 1 FROM user_tablos ut
            JOIN events ON ut.id = events.tablo_id
            WHERE events.deleted_at IS NULL
            AND (
                ut.user_id = (SELECT auth.uid())
            )
        )
    );

-- Policy to allow users to update their own events in accessible tablos
CREATE POLICY "Users can update their own events in accessible tablos" ON events
    FOR UPDATE USING (
        created_by = (SELECT auth.uid())
        AND EXISTS (
            SELECT 1 FROM user_tablos ut
            JOIN events ON ut.id = events.tablo_id
            WHERE events.deleted_at IS NULL
            AND (
                ut.user_id = (SELECT auth.uid())
            )
        )
    ) WITH CHECK (
        created_by = (SELECT auth.uid())
        AND EXISTS (
            SELECT 1 FROM user_tablos ut
            JOIN events ON ut.id = events.tablo_id
            WHERE events.deleted_at IS NULL
            AND (
                ut.user_id = (SELECT auth.uid())
            )
        )
    );

-- Policy to allow users to delete their own events in accessible tablos
CREATE POLICY "Users can delete their own events in accessible tablos" ON events
    FOR DELETE USING (
        created_by = (SELECT auth.uid())
        AND EXISTS (
            SELECT 1 FROM user_tablos ut
            JOIN events ON ut.id = events.tablo_id
            WHERE events.deleted_at IS NULL
            AND (
                ut.user_id = (SELECT auth.uid())
            )
        )
    );

-- Add comments to document the table
COMMENT ON TABLE events IS 
    'Calendar events linked to tablos with Row Level Security';

COMMENT ON COLUMN events.id IS 
    'Primary key: random 24-character alphanumeric string';

COMMENT ON COLUMN events.tablo_id IS 
    'Foreign key reference to tablos.id (24-character string)';

COMMENT ON COLUMN events.start_date IS 
    'Date of the event (YYYY-MM-DD format)';

COMMENT ON COLUMN events.start_time IS 
    'Start time of the event (HH:MM format)';

COMMENT ON COLUMN events.end_time IS 
    'End time of the event (HH:MM format), optional'; 