-- История перемещений и событий по оборудованию (закупка, выдача, перемещения, ремонты, списание)
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'office_equipment_history_type') THEN
    CREATE TYPE office_equipment_history_type AS ENUM ('purchase', 'issue', 'transfer', 'repair', 'write_off');
  END IF;
END$$;

CREATE TABLE IF NOT EXISTS office_equipment_history (
  id                BIGSERIAL PRIMARY KEY,
  equipment_id      BIGINT NOT NULL REFERENCES office_equipment(id) ON DELETE CASCADE,
  event_type        office_equipment_history_type NOT NULL,
  event_date        DATE NOT NULL,
  assigned_to       TEXT,
  assigned_from     TEXT,
  reason            TEXT,
  created_at        TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_equipment_history_equipment ON office_equipment_history(equipment_id);
CREATE INDEX IF NOT EXISTS idx_equipment_history_date ON office_equipment_history(event_date DESC);
