-- user_info
CREATE TABLE user_info
(
    `user_id`    INT           NOT NULL    AUTO_INCREMENT,
    `username`   VARCHAR(50)   NOT NULL,
    `password`   VARCHAR(50)   NOT NULL,
    `created_at` TIMESTAMP     NOT NULL    DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP     NOT NULL    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     PRIMARY KEY (user_id)
);


-- hospital_baseinfo
CREATE TABLE hospital_baseinfo
(
    `hospital_id`      CHAR(36)     NOT NULL,
    `owner_user_id`    INT          NOT NULL,
    `hospital_name`    VARCHAR(50)  NOT NULL,
    `hospital_name_en` VARCHAR(50)  NULL,
    `brn`              VARCHAR(50)  NOT NULL,
    `road_address`     VARCHAR(100) NULL,
    `site_address`     VARCHAR(100) NULL,
    `status`           VARCHAR(20)  NOT NULL    DEFAULT 'start',
    `created_at`       TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
    `updated_at`       TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     PRIMARY KEY (hospital_id)
);

CREATE INDEX IX_hospital_baseinfo_1 ON hospital_baseinfo (owner_user_id);


-- remote_source: 병원별 채널 소스 정보 (instagram/facebook/naver_blog/youtube/gangnam_unni 등)
CREATE TABLE remote_source
(
    `source_id`   INT          NOT NULL    AUTO_INCREMENT,
    `hospital_id` CHAR(36)     NOT NULL,
    `source_type` VARCHAR(50)  NOT NULL,
    `language`    CHAR(2)      NULL,
    `url`         VARCHAR(500) NOT NULL,
    `created_at`  TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (source_id)
);

CREATE INDEX IX_remote_source_1 ON remote_source (hospital_id);
CREATE INDEX IX_remote_source_2 ON remote_source (hospital_id, source_type);


-- analysis_runs
CREATE TABLE analysis_runs
(
    `analysis_run_id` CHAR(36)     NOT NULL,
    `hospital_id`     CHAR(36)     NOT NULL,
    `owner_user_id`   INT          NOT NULL    DEFAULT 0,
    `status`          VARCHAR(50)  NOT NULL    DEFAULT 'discovering',
    `report_data`     JSON         NULL,
    `plan_data`       JSON         NULL,
    `created_at`      TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
    `updated_at`      TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     PRIMARY KEY (analysis_run_id)
);

CREATE INDEX IX_analysis_runs_1 ON analysis_runs (hospital_id);
CREATE INDEX IX_analysis_runs_2 ON analysis_runs (owner_user_id);


-- raw_info: 분석 실행별 수집 원시 데이터
CREATE TABLE raw_info
(
    `info_id`         INT          NOT NULL    AUTO_INCREMENT,
    `source_id`       INT          NOT NULL,
    `analysis_run_id` CHAR(36)     NOT NULL,
    `data_tag`        VARCHAR(50)  NOT NULL    DEFAULT 'default',
    `status`          VARCHAR(20)  NOT NULL    DEFAULT 'start',
    `raw_data`        JSON         NULL,
    `logo_url`        VARCHAR(500) NULL,
    `created_at`      TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
    `updated_at`      TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     PRIMARY KEY (info_id)
);

CREATE INDEX IX_raw_info_1 ON raw_info (analysis_run_id);
CREATE INDEX IX_raw_info_2 ON raw_info (source_id);


-- file_data
CREATE TABLE file_data
(
    `id`              INT             NOT NULL    AUTO_INCREMENT,
    `analysis_run_id` CHAR(36)        NOT NULL,
    `hospital_id`     CHAR(36)        NULL,
    `file_type`       ENUM('image','video','audio','document','file') NOT NULL DEFAULT 'file',
    `file_name`       VARCHAR(255)    NOT NULL,
    `file_url`        VARCHAR(2048)   NOT NULL,
    `size_bytes`      BIGINT          NULL,
    `is_deleted`      BOOLEAN         NOT NULL    DEFAULT FALSE,
    `created_at`      TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id),
     INDEX IX_file_data_1 (analysis_run_id, is_deleted)
);


-- hospital_history
CREATE TABLE hospital_history
(
    `id`               INT          NOT NULL    AUTO_INCREMENT,
    `hospital_id`      CHAR(36)     NOT NULL,
    `owner_user_id`    INT          NOT NULL,
    `hospital_name`    VARCHAR(50)  NOT NULL,
    `hospital_name_en` VARCHAR(50)  NULL,
    `brn`              VARCHAR(50)  NOT NULL,
    `road_address`     VARCHAR(100) NULL,
    `site_address`     VARCHAR(100) NULL,
    `status`           VARCHAR(20)  NOT NULL,
    `analysis_run_id`  CHAR(36)     NULL,
    `created_at`       TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

CREATE INDEX IX_hospital_history_1 ON hospital_history (hospital_id);
CREATE INDEX IX_hospital_history_2 ON hospital_history (analysis_run_id);


-- market_analysis
CREATE TABLE market_analysis
(
    `id`              INT          NOT NULL    AUTO_INCREMENT,
    `analysis_run_id` CHAR(36)     NOT NULL,
    `analysis_type`   VARCHAR(50)  NOT NULL,
    `status`          VARCHAR(20)  NOT NULL    DEFAULT 'start',
    `data`            JSON         NULL,
    `created_at`      TIMESTAMP    NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id),
     UNIQUE KEY UQ_market_analysis (analysis_run_id, analysis_type)
);

CREATE INDEX IX_market_analysis_1 ON market_analysis (analysis_run_id);
