create table ENS_SND_DTL_KKO_TALK
(
    SEND_DETAIL_ID             NUMBER(19) not null
        primary key,
    TITLE                       VARCHAR2(40),
    LINK                        VARCHAR2(500),
    HASH                        VARCHAR2(99),
    GUIDE                       VARCHAR2(2000),
    PAYLOAD                     VARCHAR2(500),
    READ_EXPIRES_AT             VARCHAR2(20),
    REVIEW_EXPIRES_AT           VARCHAR2(20),
    USE_NON_PERSONALIZED_NOTI   VARCHAR2(10),
    CI                          VARCHAR2(88),
    PHONE_NUMBER                VARCHAR2(30),
    NAME                        VARCHAR2(50),
    BIRTHDAY                    VARCHAR2(8),
    EXTERNAL_ID                 VARCHAR2(40),
    ENVELOPE_ID                 VARCHAR2(34),
    ERROR_CODE                  VARCHAR2(40),
    ERROR_MESSAGE               CLOB,
    MK_JID                      VARCHAR2(24),
    MK_TMPLT_MSG_JSON_DATA      VARCHAR2(4000),
    LAST_UPDT_DT                TIMESTAMP(6),
    REGIST_DT                   TIMESTAMP(6),
    BILL_UID                    VARCHAR2(45 char)
        constraint FK_BILL_UID
            references ENS_BILL (BILL_UID),
    SEND_MAST_ID                NUMBER(19)
        constraint FK_SEND_MAST_ID
            references ENS_SND_MAST(SEND_MAST_ID)
);
comment on table ENS_SND_DTL_KKO_TALK is '카카오톡 상세';
comment on column ENS_SND_DTL_KKO_TALK.SEND_DETAIL_ID is '발송 상세 ID';
comment on column ENS_SND_DTL_KKO_TALK.SEND_MAST_ID is '발송 마스터 ID';
comment on column ENS_SND_DTL_KKO_TALK.TITLE is '제목';
comment on column ENS_SND_DTL_KKO_TALK.LINK is '웹링크';
comment on column ENS_SND_DTL_KKO_TALK.HASH is '해시';
comment on column ENS_SND_DTL_KKO_TALK.GUIDE is '메시지';
comment on column ENS_SND_DTL_KKO_TALK.PAYLOAD is 'PAYLOAD';
comment on column ENS_SND_DTL_KKO_TALK.READ_EXPIRES_AT is '최초 열람 만료 일시';
comment on column ENS_SND_DTL_KKO_TALK.REVIEW_EXPIRES_AT is '재열람 만료 일시';
comment on column ENS_SND_DTL_KKO_TALK.USE_NON_PERSONALIZED_NOTI is '알림톡 개인정보 제거 여부';
comment on column ENS_SND_DTL_KKO_TALK.CI is 'CI';
comment on column ENS_SND_DTL_KKO_TALK.PHONE_NUMBER is '수신인 전화번호';
comment on column ENS_SND_DTL_KKO_TALK.NAME is '수신인 이름';
comment on column ENS_SND_DTL_KKO_TALK.BIRTHDAY is '수신인 생년월일';
comment on column ENS_SND_DTL_KKO_TALK.EXTERNAL_ID is '문서 매핑 식별 ID';
comment on column ENS_SND_DTL_KKO_TALK.ENVELOPE_ID is '문서 고유 ID';
comment on column ENS_SND_DTL_KKO_TALK.STATUS is '문서 상태';
comment on column ENS_SND_DTL_KKO_TALK.SENT_AT is '문서 송신 일시';
comment on column ENS_SND_DTL_KKO_TALK.RECEIVED_AT is '문서 수신 일시';
comment on column ENS_SND_DTL_KKO_TALK.READ_AT is '문서 열람 일시';
comment on column ENS_SND_DTL_KKO_TALK.AUTHENTICATED_AT is '문서 열람 인증 일시';
comment on column ENS_SND_DTL_KKO_TALK.OTT_VERIFIED_AT is '토큰 검증 일시';
comment on column ENS_SND_DTL_KKO_TALK.IS_NOTIFICATION_UNAVAILABLE is '알림톡 수신 가능 여부';
comment on column ENS_SND_DTL_KKO_TALK.USER_NOTIFIED_AT is '알림톡 수신 일시';
comment on column ENS_SND_DTL_KKO_TALK.DISTRIBUTION_RECEIVED_AT is '유통정보 수신 일시';
comment on column ENS_SND_DTL_KKO_TALK.ERROR_CODE is '에러 코드';
comment on column ENS_SND_DTL_KKO_TALK.ERROR_MESSAGE is '에러 메시지';
comment on column ENS_SND_DTL_KKO_TALK.REGIST_DT is '등록 일시';
comment on column ENS_SND_DTL_KKO_TALK.LAST_UPDT_DT is '수정 일시';

/* FIXME: 카카오톡에 맞게 변경 필요 */
create table ENS_TMPLT_MNG_KKO_TALK
(
    CI_TRANS_USE_YN      VARCHAR2(1 char)   not null,
    CS_NAME              VARCHAR2(10 char)  not null,
    CS_NUMBER            VARCHAR2(20 char)  not null,
    PRY_MESSAGE          VARCHAR2(2000 char),
    TMPLT_CS_INFO_USE_YN VARCHAR2(1 char)   not null,
    TMPLT_MSG_USE_YN     VARCHAR2(1 char)   not null,
    ORG_CD               VARCHAR2(255 char) not null,
    TMPLT_CD             VARCHAR2(30 char)  not null,
    primary key (ORG_CD, TMPLT_CD),
    constraint FK_TMPLT_MNG_KKO_TALK
        foreign key (ORG_CD, TMPLT_CD) references ENS_TMPLT_MNG
);

create table ENS_SND_DTL_KKO_TALK_REQ_HIS
(
    ID                     NUMBER(19)          not null
        primary key,
    ENVELOPE_ID            VARCHAR2(255 char),
    EXTERNAL_ID            VARCHAR2(255 char),
    RESP_RAW_MSG           VARCHAR2(1000 char),
    SEND_DETAIL_ID         NUMBER(19),
    SEND_RAW_MSG           VARCHAR2(4000 char) not null,
    ERROR_CODE             VARCHAR2(255 char),
    ERROR_MESSAGE          CLOB,
    REGIST_DT              TIMESTAMP(6)
);
create index IDX_SND_DTL_KKO_TALK_REQ_HIS_01
    on ENS_SND_DTL_KKO_TALK_REQ_HIS (REGIST_DT);

create table ENS_SND_DTL_KKO_TALK_STAT_HIS
(
    ID                           NUMBER(19) not null
        primary key,
    ENVELOPE_ID                  VARCHAR2(255 char),
    EXTERNAL_ID                  VARCHAR2(255 char),
    AUTHENTICATED_AT             VARCHAR2(19),
    READ_AT                      VARCHAR2(19),
    READ_EXPIRED_AT              VARCHAR2(19),
    RECEIVED_AT                  VARCHAR2(19),
    OTT_VERIFIED_AT              VARCHAR2(19),
    SENT_AT                      VARCHAR2(19),
    STATUS                       VARCHAR2(255 char),
    DISTRIBUTION_RECEIVED_AT     VARCHAR2(19),
    PAYLOAD                      VARCHAR2(255 char),
    SEND_DETAIL_ID               NUMBER(19) not null,
    TOKEN_USED_AT                VARCHAR2(19),
    USER_NOTIFIED_AT             VARCHAR2(19),
    ERROR_CODE                   VARCHAR2(255 char),
    ERROR_MESSAGE                CLOB,
    REGIST_DT                    TIMESTAMP(6)
);
create index IDX_SND_DTL_KKO_TALK_STAT_HIS_01
    on ENS_SND_DTL_KKO_TALK_STAT_HIS (REGIST_DT);

create table ENS_SND_DTL_KKO_TALK_OTT_HIS
(
    ID                     NUMBER(19) not null
        primary key,
    ENVELOPE_ID            VARCHAR2(255 char),
    EXTERNAL_ID            VARCHAR2(255 char),
    AUTHENTICATED_AT       VARCHAR2(19),
    RECEIVED_AT            VARCHAR2(19),
    SENT_AT                VARCHAR2(19),
    PAYLOAD                VARCHAR2(255 char),
    SIGNED_AT              VARCHAR2(255 char),
    TOKEN                  VARCHAR2(255 char),
    TOKEN_EXPIRES_AT       VARCHAR2(19),
    TOKEN_STATUS           VARCHAR2(255 char),
    TOKEN_USED_AT          VARCHAR2(19),
    USER_NOTIFIED_AT       VARCHAR2(19),
    ERROR_CODE             VARCHAR2(255 char),
    ERROR_MESSAGE          CLOB,
    REGIST_DT              TIMESTAMP(6)
);
create index IDX_SND_DTL_KKO_TALK_OTT_HIS_01
    on ENS_SND_DTL_KKO_TALK_OTT_HIS (REGIST_DT);


ALTER TABLE ENS_ORG_MNG ADD  KAKAO_PARTNER_REST_API_KEY VARCHAR2(100);
ALTER TABLE ENS_ORG_MNG ADD  KAKAO_DEALER_REST_API_KEY VARCHAR2(100);
ALTER TABLE ENS_ORG_MNG ADD  KAKAO_DEV_SETTLE_ID VARCHAR2(100);
ALTER TABLE ENS_ORG_MNG ADD  KAKAO_PROD_SETTLE_ID VARCHAR2(100);

ALTER TABLE ENS_INTGRN_SND_DTL ADD SEND_DETAIL_ID_KKO_TALK NUMBER(19);
ALTER TABLE ENS_INTGRN_SND_DTL ADD J_ACPT_DOC_KKO_TALK CLOB;