-- migrations/0008_events.sql
-- Phase 10: Events

-- +goose Up

CREATE TABLE events (
    id          uuid        PRIMARY KEY DEFAULT gen_random_uuid(),
    tablo_id    uuid        NOT NULL REFERENCES tablos(id) ON DELETE CASCADE,
    title       text        NOT NULL,
    event_date  date        NOT NULL,
    start_time  time        NOT NULL,
    end_time    time,
    description text,
    location    text,
    created_at  timestamptz NOT NULL DEFAULT now(),
    updated_at  timestamptz NOT NULL DEFAULT now(),
    CONSTRAINT events_title_not_blank CHECK (length(trim(title)) > 0),
    CONSTRAINT events_end_after_start CHECK (end_time IS NULL OR end_time > start_time)
);

CREATE INDEX events_tablo_month_idx ON events(tablo_id, event_date, start_time, title);
CREATE INDEX events_date_idx ON events(event_date, start_time);

-- +goose Down

DROP INDEX IF EXISTS events_date_idx;
DROP INDEX IF EXISTS events_tablo_month_idx;
DROP TABLE IF EXISTS events;
