CREATE TABLE event_applications (
    uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    event_uuid UUID NOT NULL,
    club_uuid UUID NOT NULL,
    full_name TEXT NOT NULL,
    email TEXT NOT NULL,
    birth_date DATE,
    gender TEXT,
    phone TEXT,
    rating_points INTEGER,
    status TEXT NOT NULL DEFAULT 'PENDING',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_app_event FOREIGN KEY (event_uuid) REFERENCES tennis_events(uuid) ON DELETE CASCADE,
    CONSTRAINT fk_app_club  FOREIGN KEY (club_uuid)  REFERENCES tennis_clubs(uuid)  ON DELETE CASCADE
);

CREATE INDEX idx_event_applications_event_uuid ON event_applications(event_uuid);
CREATE INDEX idx_event_applications_club_uuid  ON event_applications(club_uuid);
CREATE INDEX idx_event_applications_status     ON event_applications(status);
CREATE INDEX idx_event_applications_created_at ON event_applications(created_at DESC);

CREATE TRIGGER update_event_applications_updated_at
    BEFORE UPDATE ON event_applications
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
