-- 사업관리 메뉴용 두 테이블.
--
-- contact_log: "신청 확인 연락" 이력. 요청자가 이 부분은 자료가 자주 바뀌어 이력관리가
--   필요하다고 했으므로, 한 기관에 여러 건이 쌓이고 지우지 않는 이상 남는다.
-- org_report: 기관별 최종 보고서. 같은 기관에 여러 번 올릴 수 있고(개정판) 최신순으로 본다.
--   파일은 DB에 그대로 담는다 - Mattermost 채널에 올리면 채널을 초기화하거나 아카이브할 때
--   보고서까지 같이 사라지기 때문이다.

create table contact_log (
    id           bigserial primary key,
    org_id       bigint       not null references organization (id) on delete cascade,
    contacted_on date         not null,
    method       varchar(20)  not null,   -- 전화 / 메일 / 방문 / 기타
    summary      varchar(1000) not null,
    author       varchar(100) not null,   -- 로그인 사용자
    created_at   timestamptz  not null default now()
);
create index ix_contact_log_org on contact_log (org_id, contacted_on desc, id desc);

create table org_report (
    id           bigserial primary key,
    org_id       bigint       not null references organization (id) on delete cascade,
    file_name    varchar(300) not null,
    content_type varchar(200),
    byte_size    bigint       not null,
    content      bytea        not null,
    uploaded_by  varchar(100) not null,
    uploaded_at  timestamptz  not null default now()
);
create index ix_org_report_org on org_report (org_id, uploaded_at desc);
