-- Справочник статей доходов и расходов для платежного календаря
CREATE TABLE IF NOT EXISTS payment_categories (
  id          BIGSERIAL PRIMARY KEY,
  name        TEXT NOT NULL,
  code        VARCHAR(50),
  direction   VARCHAR(20) NOT NULL CHECK (direction IN ('income', 'expense')),
  parent_id   BIGINT REFERENCES payment_categories(id) ON DELETE SET NULL,
  is_active   BOOLEAN NOT NULL DEFAULT TRUE,
  sort_order  INTEGER DEFAULT 0,
  created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_payment_categories_direction ON payment_categories(direction);
CREATE INDEX IF NOT EXISTS idx_payment_categories_parent ON payment_categories(parent_id);
CREATE INDEX IF NOT EXISTS idx_payment_categories_active ON payment_categories(is_active);

COMMENT ON TABLE payment_categories IS 'Справочник статей доходов и расходов для платежного календаря';
