BEGIN;

CREATE TABLE network_stripe_customers (
  network_id         TEXT PRIMARY KEY,
  stripe_customer_id TEXT NOT NULL UNIQUE,
  created_at         TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE network_subscriptions (
  id                     TEXT        PRIMARY KEY,
  network_id             TEXT        NOT NULL REFERENCES network_stripe_customers(network_id) ON DELETE CASCADE,
  stripe_customer_id     TEXT        NOT NULL,
  status                 TEXT        NOT NULL,
  price_id               TEXT        NOT NULL,
  cadence                TEXT        NOT NULL,
  quantity               INT         NOT NULL,
  cancel_at_period_end   BOOLEAN     NOT NULL DEFAULT FALSE,
  current_period_start   TIMESTAMPTZ NOT NULL,
  current_period_end     TIMESTAMPTZ NOT NULL,
  created_at             TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at             TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  CONSTRAINT network_subscriptions_cadence_check CHECK (cadence IN ('monthly', 'annual'))
);

CREATE UNIQUE INDEX idx_network_subscriptions_network_id
  ON network_subscriptions (network_id);

COMMIT;
