-- Migration: 025_add_analytics_events
-- Creates the analytics_events table for daily pre-aggregated event rollups.
-- Each row represents one (tenant, date, metric_type) bucket with a running count.

CREATE TABLE IF NOT EXISTS analytics_events (
  organization_id VARCHAR(40)  NOT NULL REFERENCES organizations(organization_id) ON DELETE CASCADE,
  date            DATE         NOT NULL,
  metric_type     VARCHAR(50)  NOT NULL,
  count           INTEGER      NOT NULL DEFAULT 0,
  PRIMARY KEY (organization_id, date, metric_type)
);

CREATE INDEX IF NOT EXISTS idx_analytics_events_org_date
  ON analytics_events(organization_id, date);
