-- batch_history 테이블 생성
CREATE TABLE IF NOT EXISTS public.batch_history (
    uuid UUID PRIMARY KEY,
    job VARCHAR(255) NOT NULL,
    id VARCHAR(255) NOT NULL,
    created_dttm TIMESTAMP NOT NULL,
    updated_dttm TIMESTAMP NOT NULL,
    status VARCHAR(50) NOT NULL,
    completed_dttm TIMESTAMP
);

-- 인덱스 생성 (조회 성능 향상)
CREATE INDEX IF NOT EXISTS idx_batch_history_job ON public.batch_history(job);
CREATE INDEX IF NOT EXISTS idx_batch_history_status ON public.batch_history(status);
CREATE INDEX IF NOT EXISTS idx_batch_history_created ON public.batch_history(created_dttm DESC);

-- 코멘트
COMMENT ON TABLE public.batch_history IS '배치 작업 실행 이력';
COMMENT ON COLUMN public.batch_history.uuid IS '배치 실행 고유 ID';
COMMENT ON COLUMN public.batch_history.job IS '배치 작업 이름';
COMMENT ON COLUMN public.batch_history.id IS '비즈니스 ID';
COMMENT ON COLUMN public.batch_history.created_dttm IS '생성 일시';
COMMENT ON COLUMN public.batch_history.updated_dttm IS '수정 일시';
COMMENT ON COLUMN public.batch_history.status IS '상태 (STARTED/COMPLETED/FAILED)';
COMMENT ON COLUMN public.batch_history.completed_dttm IS '완료 일시';

-- batch_step_history 테이블 생성
CREATE TABLE IF NOT EXISTS public.batch_step_history (
    id BIGSERIAL PRIMARY KEY,
    anal_uid BIGINT NOT NULL,
    result_uid VARCHAR(255) NOT NULL,
    step_name VARCHAR(100) NOT NULL,
    status VARCHAR(50) NOT NULL,
    error_message TEXT,
    started_dttm TIMESTAMP NOT NULL,
    completed_dttm TIMESTAMP,
    created_dttm TIMESTAMP NOT NULL,
    updated_dttm TIMESTAMP NOT NULL
);

-- 인덱스 생성
CREATE INDEX IF NOT EXISTS idx_batch_step_history_anal_uid ON public.batch_step_history(anal_uid);
CREATE INDEX IF NOT EXISTS idx_batch_step_history_result_uid ON public.batch_step_history(result_uid);
CREATE INDEX IF NOT EXISTS idx_batch_step_history_status ON public.batch_step_history(status);
CREATE INDEX IF NOT EXISTS idx_batch_step_history_step_name ON public.batch_step_history(step_name);

-- 코멘트
COMMENT ON TABLE public.batch_step_history IS '배치 Step 실행 이력';
COMMENT ON COLUMN public.batch_step_history.id IS 'Step 이력 고유 ID';
COMMENT ON COLUMN public.batch_step_history.anal_uid IS '분석 UID';
COMMENT ON COLUMN public.batch_step_history.result_uid IS '결과 UID';
COMMENT ON COLUMN public.batch_step_history.step_name IS 'Step 이름 (makeGeoJsonStep/dockerRunStep/zipResponseStep)';
COMMENT ON COLUMN public.batch_step_history.status IS '상태 (STARTED/SUCCESS/FAILED)';
COMMENT ON COLUMN public.batch_step_history.error_message IS '에러 메시지';
COMMENT ON COLUMN public.batch_step_history.started_dttm IS 'Step 시작 일시';
COMMENT ON COLUMN public.batch_step_history.completed_dttm IS 'Step 완료 일시';
COMMENT ON COLUMN public.batch_step_history.created_dttm IS '생성 일시';
COMMENT ON COLUMN public.batch_step_history.updated_dttm IS '수정 일시';
