CREATE TABLE IF NOT EXISTS obligacion_periodos (
  id              uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  obligacion_id   uuid NOT NULL REFERENCES obligaciones_contribuyente(id) ON DELETE CASCADE,
  periodo         varchar(7) NOT NULL,
  completada      boolean DEFAULT false,
  completada_at   timestamptz,
  completada_por  uuid,
  notas           text,
  created_at      timestamptz DEFAULT now(),
  UNIQUE (obligacion_id, periodo)
);

CREATE INDEX IF NOT EXISTS ix_obligacion_periodos_periodo ON obligacion_periodos(periodo);

INSERT INTO tenant_migrations (scope, version, name)
VALUES ('vertical-contable', 20, '020_obligacion_periodos')
ON CONFLICT (scope, version) DO NOTHING;
