-- 003_core_project.sql — core.project + core.stage

CREATE TABLE core.project (
    id bigserial PRIMARY KEY,
    name text NOT NULL,
    bitra_id text UNIQUE,
    eva_id text UNIQUE,
    bitra_code text,
    eva_code text,
    is_sd boolean DEFAULT false,           -- SD-проект (из явного списка)
    status text,
    cache_status_type text,                 -- из EVA: OPEN/IN_PROGRESS/IN_REVIEW/CLOSED
    project_manager_id bigint REFERENCES core.employee,
    bitrix_company_id bigint,
    bitra_client_id text,
    bitra_client_name text,                 -- для отображения
    deadline date,
    budget decimal(15,2),
    last_synced timestamptz
);

CREATE INDEX idx_project_bitra ON core.project (bitra_id);
CREATE INDEX idx_project_eva ON core.project (eva_id);
CREATE INDEX idx_project_sd ON core.project (is_sd) WHERE is_sd = true;

CREATE TABLE core.stage (
    id bigserial PRIMARY KEY,
    name text NOT NULL,
    bitra_id text UNIQUE,
    project_id bigint REFERENCES core.project,
    plan_start_date date,
    plan_end_date date,
    is_completed boolean DEFAULT false,
    is_acted boolean DEFAULT false
);

CREATE INDEX idx_stage_project ON core.stage (project_id);
