-- Unified announcements table for all SEAP data types
BEGIN;

CREATE TABLE IF NOT EXISTS seap.announcements (
  id              BIGSERIAL PRIMARY KEY,
  type            TEXT NOT NULL,
  ref_number      TEXT NOT NULL,
  authority_name  TEXT,
  authority_cui   TEXT,
  authority_siruta TEXT,
  title           TEXT,
  cpv_code        TEXT,
  cpv_name        TEXT,
  contract_type   TEXT,
  publication_date TIMESTAMPTZ,
  finalization_date TIMESTAMPTZ,
  contract_date    DATE,
  estimated_value  NUMERIC(15,2),
  awarded_value    NUMERIC(15,2),
  currency         TEXT DEFAULT 'RON',
  supplier_name    TEXT,
  supplier_cui     TEXT,
  supplier_siruta  TEXT,
  procedure_type   TEXT,
  procedure_state  TEXT,
  award_type       TEXT,
  legislation      TEXT,
  criterion        TEXT,
  eu_funded        TEXT,
  eu_program       TEXT,
  lot_number       INTEGER,
  has_lots         TEXT,
  joue             TEXT,
  value_before     NUMERIC(15,2),
  value_after      NUMERIC(15,2),
  modification_desc TEXT,
  seap_url         TEXT,
  source           TEXT DEFAULT 'datagov',
  imported_at      TIMESTAMPTZ DEFAULT now(),
  UNIQUE(type, ref_number)
);

CREATE INDEX IF NOT EXISTS idx_ann_type ON seap.announcements(type);
CREATE INDEX IF NOT EXISTS idx_ann_auth_cui ON seap.announcements(authority_cui);
CREATE INDEX IF NOT EXISTS idx_ann_auth_siruta ON seap.announcements(authority_siruta);
CREATE INDEX IF NOT EXISTS idx_ann_sup_cui ON seap.announcements(supplier_cui);
CREATE INDEX IF NOT EXISTS idx_ann_pub_date ON seap.announcements(publication_date);
CREATE INDEX IF NOT EXISTS idx_ann_value ON seap.announcements(awarded_value);
CREATE INDEX IF NOT EXISTS idx_ann_cpv ON seap.announcements(cpv_code);
CREATE INDEX IF NOT EXISTS idx_ann_contract_type ON seap.announcements(contract_type);

COMMIT;
