-- 1. 상담 설정 테이블
CREATE TABLE IF NOT EXISTS `consultant_config` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `config_key` varchar(100) NOT NULL COMMENT '설정 키',
    `config_value` text COMMENT '설정 값',
    `config_desc` varchar(255) DEFAULT NULL COMMENT '설정 설명',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 시스템 설정';

-- 2. 상담 일정 테이블
CREATE TABLE IF NOT EXISTS `consultant_schedule` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `specific_date` date DEFAULT NULL COMMENT '특정 날짜',
    `start_time` time NOT NULL COMMENT '시작 시간',
    `end_time` time NOT NULL COMMENT '종료 시간',
    `time_slot` int(11) DEFAULT 60 COMMENT '예약 단위 시간(분)',
    `max_persons` int(11) DEFAULT 1 COMMENT '동시간대 최대 예약 인원',
    `is_available` tinyint(1) DEFAULT 1 COMMENT '예약 가능 여부',
    `temp_1` varchar(255) DEFAULT NULL COMMENT '임시필드1',
    `temp_2` varchar(255) DEFAULT NULL COMMENT '임시필드2',
    `temp_3` varchar(255) DEFAULT NULL COMMENT '임시필드3',
    `temp_4` varchar(255) DEFAULT NULL COMMENT '임시필드4',
    `temp_5` varchar(255) DEFAULT NULL COMMENT '임시필드5',
    `extra_1` varchar(255) DEFAULT NULL COMMENT '여분필드1',
    `extra_2` varchar(255) DEFAULT NULL COMMENT '여분필드2',
    `extra_3` varchar(255) DEFAULT NULL COMMENT '여분필드3',
    `extra_4` varchar(255) DEFAULT NULL COMMENT '여분필드4',
    `extra_5` varchar(255) DEFAULT NULL COMMENT '여분필드5',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_specific_date` (`specific_date`),
    KEY `idx_is_available` (`is_available`),
    KEY `idx_time_range` (`start_time`,`end_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 상세 스케줄';

-- 3. 상담 예약 테이블
CREATE TABLE IF NOT EXISTS `consultant_reservations` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `wr_id` int(11) DEFAULT NULL COMMENT '연결된 게시글 ID',
    `customer_name` varchar(100) NOT NULL COMMENT '고객명',
    `customer_phone` varchar(20) NOT NULL COMMENT '고객 연락처',
    `customer_email` varchar(100) DEFAULT NULL COMMENT '고객 이메일',
    `reservation_date` date NOT NULL COMMENT '예약 날짜',
    `reservation_time` time NOT NULL COMMENT '예약 시간',
    `consultation_type` varchar(50) DEFAULT 'onsite' COMMENT '상담 유형',
    `resource_id` int(11) DEFAULT NULL COMMENT '배정된 리소스 ID',
    `status` varchar(50) DEFAULT 'payment_pending' COMMENT '예약 상태',
    `payment_amount` int(11) DEFAULT 0 COMMENT '상담 비용',
    `payment_status` varchar(50) DEFAULT 'pending' COMMENT '결제 상태',
    `payment_confirmed_at` datetime DEFAULT NULL COMMENT '입금 확인 시간',
    `payment_confirmed_by` varchar(50) DEFAULT NULL COMMENT '입금 확인자',
    `request_memo` text COMMENT '고객 요청사항',
    `admin_memo` text COMMENT '관리자 메모',
    `is_deleted` tinyint(1) DEFAULT 0 COMMENT '삭제 여부',
    `temp_1` varchar(255) DEFAULT NULL COMMENT '임시필드1',
    `temp_2` varchar(255) DEFAULT NULL COMMENT '임시필드2',
    `temp_3` varchar(255) DEFAULT NULL COMMENT '임시필드3',
    `temp_4` varchar(255) DEFAULT NULL COMMENT '임시필드4',
    `temp_5` varchar(255) DEFAULT NULL COMMENT '임시필드5',
    `extra_1` varchar(255) DEFAULT NULL COMMENT '여분필드1',
    `extra_2` varchar(255) DEFAULT NULL COMMENT '여분필드2',
    `extra_3` varchar(255) DEFAULT NULL COMMENT '여분필드3',
    `extra_4` varchar(255) DEFAULT NULL COMMENT '여분필드4',
    `extra_5` varchar(255) DEFAULT NULL COMMENT '여분필드5',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_reservation_date` (`reservation_date`),
    KEY `idx_status` (`status`),
    KEY `idx_customer_phone` (`customer_phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 정보';

-- 4. 상담 메일 알림 템플릿 테이블
CREATE TABLE IF NOT EXISTS `consultant_mail_templates` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `template_key` varchar(100) NOT NULL,
    `template_type` varchar(10) NOT NULL DEFAULT 'email' COMMENT '템플릿 종류 (email)',
    `template_name` varchar(200) NOT NULL,
    `template_subject` varchar(255) NOT NULL,
    `template_content` text NOT NULL,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `template_key` (`template_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 메일 알림 템플릿';

-- 5. 상담 문자 알림 템플릿 테이블
CREATE TABLE IF NOT EXISTS `consultant_sms_templates` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `template_key` varchar(100) NOT NULL,
    `template_type` varchar(10) NOT NULL DEFAULT 'sms' COMMENT '템플릿 종류 (sms)',
    `template_name` varchar(200) NOT NULL,
    `template_subject` varchar(255) NOT NULL,
    `template_content` text NOT NULL,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `template_key` (`template_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 문자 알림 템플릿';

-- 6. 시스템 로그 테이블
CREATE TABLE IF NOT EXISTS `consultant_log` (
    `log_id` int(11) NOT NULL AUTO_INCREMENT,
    `log_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `mb_id` varchar(20) DEFAULT NULL,
    `log_level` varchar(20) NOT NULL DEFAULT 'info',
    `log_message` text NOT NULL,
    `ip_address` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`log_id`),
    KEY `idx_log_time` (`log_time`),
    KEY `idx_mb_id` (`mb_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='상담 예약 시스템 로그';

-- 7. 상담 그룹 테이블
CREATE TABLE IF NOT EXISTS `consultant_groups` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL COMMENT '그룹명',
    `is_active` tinyint(1) DEFAULT 1 COMMENT '사용여부',
    `temp_1` varchar(255) DEFAULT NULL COMMENT '임시필드1',
    `temp_2` varchar(255) DEFAULT NULL COMMENT '임시필드2',
    `temp_3` varchar(255) DEFAULT NULL COMMENT '임시필드3',
    `temp_4` varchar(255) DEFAULT NULL COMMENT '임시필드4',
    `temp_5` varchar(255) DEFAULT NULL COMMENT '임시필드5',
    `extra_1` varchar(255) DEFAULT NULL COMMENT '여분필드1',
    `extra_2` varchar(255) DEFAULT NULL COMMENT '여분필드2',
    `extra_3` varchar(255) DEFAULT NULL COMMENT '여분필드3',
    `extra_4` varchar(255) DEFAULT NULL COMMENT '여분필드4',
    `extra_5` varchar(255) DEFAULT NULL COMMENT '여분필드5',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci  COMMENT='상담/자원 그룹';

-- 8. 상담 리소스 테이블
CREATE TABLE IF NOT EXISTS `consultant_resources` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `group_id` int(11) NOT NULL COMMENT '그룹 ID',
    `name` varchar(100) NOT NULL COMMENT '리소스명(이름/호실)',
    `description` varchar(255) DEFAULT NULL COMMENT '설명',
    `is_active` tinyint(1) DEFAULT 1 COMMENT '사용여부',
    `temp_1` varchar(255) DEFAULT NULL COMMENT '임시필드1',
    `temp_2` varchar(255) DEFAULT NULL COMMENT '임시필드2',
    `temp_3` varchar(255) DEFAULT NULL COMMENT '임시필드3',
    `temp_4` varchar(255) DEFAULT NULL COMMENT '임시필드4',
    `temp_5` varchar(255) DEFAULT NULL COMMENT '임시필드5',
    `extra_1` varchar(255) DEFAULT NULL COMMENT '여분필드1',
    `extra_2` varchar(255) DEFAULT NULL COMMENT '여분필드2',
    `extra_3` varchar(255) DEFAULT NULL COMMENT '여분필드3',
    `extra_4` varchar(255) DEFAULT NULL COMMENT '여분필드4',
    `extra_5` varchar(255) DEFAULT NULL COMMENT '여분필드5',
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='상담사/자원 목록';