-- 테이블 순서는 관계를 고려하여 한 번에 실행해도 에러가 발생하지 않게 정렬되었습니다.

-- instagram_data Table Create SQL
-- 테이블 생성 SQL - instagram_data
CREATE TABLE instagram_data
(
    `id`           INT             NOT NULL    AUTO_INCREMENT,
    `hospital_id`  CHAR(36)        NOT NULL,
    `url`          VARCHAR(500)    NOT NULL,
    `status`       VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_data`     JSON            NULL,
    `created_at`   TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - instagram_data(hospital_id)
CREATE INDEX IX_instagram_data_1
    ON instagram_data(hospital_id);


-- facebook_data Table Create SQL
-- 테이블 생성 SQL - facebook_data
CREATE TABLE facebook_data
(
    `id`           INT             NOT NULL    AUTO_INCREMENT,
    `hospital_id`  CHAR(36)        NOT NULL,
    `url`          VARCHAR(500)    NOT NULL,
    `status`       VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_data`     JSON            NULL,
    `created_at`   TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - facebook_data(hospital_id)
CREATE INDEX IX_facebook_data_1
    ON facebook_data(hospital_id);


-- naver_blog_data Table Create SQL
-- 테이블 생성 SQL - naver_blog_data
CREATE TABLE naver_blog_data
(
    `id`           INT             NOT NULL    AUTO_INCREMENT,
    `hospital_id`  CHAR(36)        NOT NULL,
    `url`          VARCHAR(500)    NOT NULL,
    `status`       VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_data`     JSON            NULL,
    `created_at`   TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - naver_blog_data(hospital_id)
CREATE INDEX IX_naver_blog_data_1
    ON naver_blog_data(hospital_id);


-- hospital_baseinfo Table Create SQL
-- 테이블 생성 SQL - 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,
    `url`            VARCHAR(500)    NULL,
    `status`         VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_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 (hospital_id)
);

-- Index 설정 SQL - hospital_baseinfo(owner_user_id)
CREATE INDEX IX_hospital_baseinfo_1
    ON hospital_baseinfo(owner_user_id);


-- user_info Table Create SQL
-- 테이블 생성 SQL - 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)
);

-- youtube_data Table Create SQL
CREATE TABLE youtube_data
(
    `id`           INT             NOT NULL    AUTO_INCREMENT,
    `hospital_id`  CHAR(36)        NOT NULL,
    `url`          VARCHAR(500)    NOT NULL,
    `status`       VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_data`     JSON            NULL,
    `created_at`   TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - youtube_data(hospital_id)
CREATE INDEX IX_youtube_data_1
    ON youtube_data(hospital_id);


-- gangnam_unni_data Table Create SQL
CREATE TABLE gangnam_unni_data
(
    `id`           INT             NOT NULL    AUTO_INCREMENT,
    `hospital_id`  CHAR(36)        NOT NULL,
    `url`          VARCHAR(500)    NOT NULL,
    `status`       VARCHAR(20)     NOT NULL    DEFAULT 'start',
    `raw_data`     JSON            NULL,
    `created_at`   TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - gangnam_unni_data(hospital_id)
CREATE INDEX IX_gangnam_unni_data_1
    ON gangnam_unni_data(hospital_id);


-- analysis_runs Table Create SQL
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',
    `instagram_data_id`    INT          NULL,
    `facebook_data_id`     INT          NULL,
    `naver_blog_data_id`   INT          NULL,
    `youtube_data_id`      INT          NULL,
    `gangnam_unni_data_id` INT          NULL,
    `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)
);

-- Index 설정 SQL - analysis_runs(hospital_id)
CREATE INDEX IX_analysis_runs_1
    ON analysis_runs(hospital_id);

-- Index 설정 SQL - analysis_runs(owner_user_id)
CREATE INDEX IX_analysis_runs_2
    ON analysis_runs(owner_user_id);


-- hospital_history Table Create SQL
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,
    `url`              VARCHAR(500)    NULL,
    `status`           VARCHAR(20)     NOT NULL,
    `raw_data`         JSON            NULL,
    `analysis_run_id`  CHAR(36)        NULL,
    `created_at`       TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
     PRIMARY KEY (id)
);

-- Index 설정 SQL - hospital_history(hospital_id)
CREATE INDEX IX_hospital_history_1
    ON hospital_history(hospital_id);

-- Index 설정 SQL - hospital_history(analysis_run_id)
CREATE INDEX IX_hospital_history_2
    ON hospital_history(analysis_run_id);


-- market_analysis Table Create SQL
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)
);

-- Index 설정 SQL - market_analysis(analysis_run_id)
CREATE INDEX IX_market_analysis_1
    ON market_analysis(analysis_run_id);

