DROP TABLE IF EXISTS logger;

CREATE TABLE
    logger (
        uid int(5) unsigned NOT NULL AUTO_INCREMENT,
        user_uid int(5) unsigned NOT NULL COMMENT 'user_uid',
        title varchar(255) NOT NULL COMMENT 'title',
        content text NOT NULL COMMENT '내용',
        status varchar(10) NOT NULL DEFAULT 'use',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        CONSTRAINT logger_ibfk_1 FOREIGN KEY (user_uid) REFERENCES user (uid) ON DELETE CASCADE
    ) ENGINE = MyISAM DEFAULT CHARSET = utf8 COMMENT = 'log 정보';

DROP TABLE IF EXISTS mapurl;

CREATE TABLE
    mapurl (
        uid int(10) unsigned NOT NULL AUTO_INCREMENT,
        oldurl varchar(255) NOT NULL COMMENT '기존 URL',
        newurl varchar(255) NOT NULL COMMENT '신규 URL',
        status varchar(10) NOT NULL DEFAULT 'use',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        UNIQUE KEY oldurl (oldurl)
    ) ENGINE = MyISAM DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = 'MapURL 정보';

DROP TABLE IF EXISTS cloudflareauth;

CREATE TABLE
    cloudflareauth (
        uid int(10) unsigned NOT NULL AUTO_INCREMENT,
        id varchar(50) NOT NULL COMMENT 'CloudFlare 계정ID',
        authkey varchar(255) NOT NULL COMMENT '인증키',
        oldkey varchar(255) DEFAULT NULL COMMENT '이전키',
        status varchar(10) NOT NULL DEFAULT 'use',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        UNIQUE KEY authkey (authkey)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = 'cloudflare 인증 정보';

DROP TABLE IF EXISTS cloudflareaccount;

CREATE TABLE
    cloudflareaccount (
        uid varchar(255) NOT NULL COMMENT 'apikey',
        auth_uid int(10) unsigned NOT NULL,
        title varchar(150) NOT NULL COMMENT 'CloudFlare 계정ID',
        type varchar(20) NOT NULL DEFAULT 'standard' COMMENT '형식',
        status varchar(10) NOT NULL DEFAULT 'use',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        CONSTRAINT cloudflareaccount_ibfk_1 FOREIGN KEY (auth_uid) REFERENCES cloudflareauth (uid)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci COMMENT = 'cloudflare 계정 정보';

DROP TABLE IF EXISTS cloudflarezone;

CREATE TABLE
    cloudflarezone (
        uid varchar(255) NOT NULL COMMENT 'apikey',
        account_uid varchar(255) NOT NULL COMMENT 'account_uid',
        domain varchar(30) NOT NULL COMMENT 'zone Domin Name',
        name_servers varchar(60) DEFAULT NULL COMMENT 'Name Servers',
        original_name_servers varchar(60) DEFAULT NULL COMMENT 'Origin Name Servers',
        plan varchar(80) NOT NULL DEFAULT 'free',
        development_mode varchar(30) NOT NULL DEFAULT 'off',
        ipv6 varchar(20) NOT NULL DEFAULT 'off',
        security_level varchar(20) NOT NULL DEFAULT 'off' COMMENT '공격방어',
        status varchar(10) NOT NULL DEFAULT 'active' COMMENT '서비스',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        UNIQUE KEY domainbyaccount (account_uid, domain),
        CONSTRAINT cloudflarezone_ibfk_1 FOREIGN KEY (account_uid) REFERENCES cloudflareaccount (uid)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT = 'cloudflare zone 정보';

DROP TABLE IF EXISTS cloudflarerecord;

CREATE TABLE
    cloudflarerecord (
        uid varchar(255) NOT NULL COMMENT 'apikey',
        zone_uid varchar(255) NOT NULL COMMENT 'zone_uid',
        type varchar(10) NOT NULL DEFAULT 'A' NULL COMMENT 'DNS TYPE',
        host varchar(60) NOT NULL COMMENT 'record Name',
        content varchar(255) NOT NULL COMMENT 'IP 또는 내용',
        ttl int(5) NOT NULL DEFAULT 1 COMMENT 'TTL 시간',
        proxiable varchar(20) NOT NULL DEFAULT 'off' COMMENT 'CDN기능여부',
        proxied varchar(20) NOT NULL DEFAULT 'off' COMMENT 'CDN기능여부2',
        fixed varchar(20) NOT NULL DEFAULT 'off' COMMENT 'Fixed CDN',
        locked varchar(20) NOT NULL DEFAULT 'off' COMMENT '서비스',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid),
        CONSTRAINT cloudflarerecord_ibfk_1 FOREIGN KEY (zone_uid) REFERENCES cloudflarezone (uid) ON DELETE CASCADE
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT = 'cloudflare record정보';

DROP TABLE IF EXISTS cloudflarefixedrecord;
CREATE TABLE
    cloudflarefixedrecord (
        uid int(10) unsigned NOT NULL AUTO_INCREMENT,
        host varchar(60) NOT NULL COMMENT 'record Name',
        updated_at timestamp NULL DEFAULT NULL,
        created_at timestamp NOT NULL DEFAULT current_timestamp(),
        PRIMARY KEY (uid)
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT = 'cloudflare fixed record정보';

DROP TABLE IF EXISTS cloudflarefirewall;

CREATE TABLE
    cloudflarefirewall (
        uid varchar(255) NOT NULL COMMENT 'rule_id',
        zone_uid varchar(255) NOT NULL COMMENT 'zone_uid',
        description varchar(255) NOT NULL COMMENT '내용',
        filter_id varchar(255) NOT NULL COMMENT 'Filter ID',
        filter_expression text NOT NULL COMMENT 'Filter RULE',
        filter_paused varchar(10) NOT NULL DEFAULT 'off' COMMENT 'Filter true/false',
        paused varchar(10) NOT NULL DEFAULT 'on' COMMENT '실제 false/true',
        action varchar(20) NOT NULL DEFAULT 'log' COMMENT 'block|allow|challenge|js_challenge|log',
        updated_at timestamp NULL DEFAULT NULL COMMENT 'modified_on',
        created_at timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'created_on',
        PRIMARY KEY (uid),
        CONSTRAINT cloudflarerecord_ibfk_2 FOREIGN KEY (zone_uid) REFERENCES cloudflarezone (uid)  ON DELETE CASCADE
    ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT = 'cloudflare firewall정보';