-- GeoJSON 모니터링 시스템을 위한 필수 테이블 생성 스크립트
-- dump-kamco_cds-202511201730.sql에서 추출

-- 1. 시퀀스 생성
CREATE SEQUENCE IF NOT EXISTS public.tb_map_sheet_learn_data_data_uid
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

-- 2. tb_map_sheet_learn_data 테이블 생성
CREATE TABLE IF NOT EXISTS public.tb_map_sheet_learn_data (
    data_uid bigint DEFAULT nextval('public.tb_map_sheet_learn_data_data_uid'::regclass) NOT NULL,
    data_name character varying(128),
    data_path character varying(255),
    data_type character varying(128),
    data_crs_type character varying(128),
    data_crs_type_name character varying(255),
    created_dttm timestamp without time zone DEFAULT now(),
    created_uid bigint,
    updated_dttm timestamp without time zone DEFAULT now(),
    updated_uid bigint,
    compare_yyyy integer,
    data_yyyy integer,
    data_json json,
    data_state character varying(20),
    data_state_dttm timestamp without time zone DEFAULT now(),
    data_title character varying(255),
    anal_map_sheet character varying(255),
    anal_strt_dttm timestamp without time zone,
    anal_end_dttm time without time zone,
    anal_sec bigint,
    gukuin_used character varying(20),
    gukuin_used_dttm timestamp without time zone,
    anal_state character varying(20),
    CONSTRAINT tb_map_sheet_learn_data_pkey PRIMARY KEY (data_uid)
);

-- 3. 시퀀스 생성 (Geometry용)
CREATE SEQUENCE IF NOT EXISTS public.tb_map_sheet_learn_data_geom_geom_uid
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

-- 4. tb_map_sheet_learn_data_geom 테이블 생성
CREATE TABLE IF NOT EXISTS public.tb_map_sheet_learn_data_geom (
    geo_uid bigint DEFAULT nextval('public.tb_map_sheet_learn_data_geom_geom_uid'::regclass) NOT NULL,
    cd_prob double precision,
    class_before_name character varying(100),
    class_before_prob double precision,
    class_after_name character varying(100),
    class_after_prob double precision,
    map_sheet_num bigint,
    before_yyyy integer,
    after_yyyy integer,
    area double precision,
    geom public.geometry,
    geo_type character varying(100),
    data_uid bigint,
    created_dttm timestamp without time zone,
    created_uid bigint,
    updated_dttm timestamp without time zone,
    updated_uid bigint,
    CONSTRAINT tb_map_sheet_learn_data_geom_pkey PRIMARY KEY (geo_uid)
);

-- 5. 외래 키 제약 조건
ALTER TABLE ONLY public.tb_map_sheet_learn_data_geom 
    ADD CONSTRAINT fk_learn_data_geom_data_uid 
    FOREIGN KEY (data_uid) REFERENCES public.tb_map_sheet_learn_data(data_uid) ON DELETE CASCADE;

-- 6. 인덱스 생성
CREATE INDEX IF NOT EXISTS idx_tb_map_sheet_learn_data_data_state ON public.tb_map_sheet_learn_data(data_state);
CREATE INDEX IF NOT EXISTS idx_tb_map_sheet_learn_data_anal_state ON public.tb_map_sheet_learn_data(anal_state);
CREATE INDEX IF NOT EXISTS idx_tb_map_sheet_learn_data_data_path ON public.tb_map_sheet_learn_data(data_path);
CREATE INDEX IF NOT EXISTS idx_tb_map_sheet_learn_data_geom_data_uid ON public.tb_map_sheet_learn_data_geom(data_uid);
CREATE INDEX IF NOT EXISTS idx_tb_map_sheet_learn_data_geom_geo_type ON public.tb_map_sheet_learn_data_geom(geo_type);

-- 7. 테이블 코멘트
COMMENT ON TABLE public.tb_map_sheet_learn_data IS '학습데이터';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.data_uid IS '식별키';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.data_name IS '데이타명';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.data_path IS '경로';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.data_type IS '타입';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.data_state IS '처리상태';
COMMENT ON COLUMN public.tb_map_sheet_learn_data.anal_state IS '분석상태';

COMMENT ON TABLE public.tb_map_sheet_learn_data_geom IS '학습데이터GEOM정보';
COMMENT ON COLUMN public.tb_map_sheet_learn_data_geom.geo_uid IS '식별키';
COMMENT ON COLUMN public.tb_map_sheet_learn_data_geom.geom IS 'geometry정보';
COMMENT ON COLUMN public.tb_map_sheet_learn_data_geom.data_uid IS '데이터식별키';

-- 완료 메시지
SELECT 'GeoJSON 모니터링 시스템 테이블 생성 완료' as message;