-- 설비 외부반출 테이블 생성 및 상태 ENUM 확장
-- 실행: docker exec -i tkfb_db mysql -u hyungi -p'your_password' hyungi < db/migrations/20260205003000_create_equipment_external_logs.sql

SET @dbname = DATABASE();

-- ============================================
-- STEP 1: equipment_external_logs 테이블 생성
-- ============================================

CREATE TABLE IF NOT EXISTS equipment_external_logs (
  log_id INT AUTO_INCREMENT PRIMARY KEY,
  equipment_id INT UNSIGNED NOT NULL COMMENT '설비 ID',
  log_type ENUM('export', 'return') NOT NULL COMMENT '반출/반입',
  export_date DATE COMMENT '반출일',
  expected_return_date DATE COMMENT '반입 예정일',
  actual_return_date DATE COMMENT '실제 반입일',
  destination VARCHAR(200) COMMENT '반출처 (수리업체명 등)',
  reason TEXT COMMENT '반출 사유',
  notes TEXT COMMENT '비고',
  exported_by INT COMMENT '반출 담당자',
  returned_by INT COMMENT '반입 담당자',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_eel_equipment FOREIGN KEY (equipment_id)
    REFERENCES equipments(equipment_id) ON DELETE CASCADE,
  CONSTRAINT fk_eel_exported_by FOREIGN KEY (exported_by)
    REFERENCES users(user_id) ON DELETE SET NULL,
  CONSTRAINT fk_eel_returned_by FOREIGN KEY (returned_by)
    REFERENCES users(user_id) ON DELETE SET NULL,
  INDEX idx_eel_equipment_id (equipment_id),
  INDEX idx_eel_log_type (log_type),
  INDEX idx_eel_export_date (export_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SELECT 'equipment_external_logs 테이블 생성 완료' AS status;

-- ============================================
-- STEP 2: equipments 테이블 status ENUM 확장
-- ============================================

-- 현재 status 컬럼의 ENUM 값 확인 후 확장
-- 기존: active, maintenance, repair_needed, inactive
-- 추가: external (외부 반출), repair_external (수리 외주)

ALTER TABLE equipments
MODIFY COLUMN status ENUM(
  'active',           -- 정상 가동
  'maintenance',      -- 점검 중
  'repair_needed',    -- 수리 필요
  'inactive',         -- 비활성
  'external',         -- 외부 반출
  'repair_external'   -- 수리 외주 (외부 수리)
) DEFAULT 'active' COMMENT '설비 상태';

SELECT 'equipments status ENUM 확장 완료' AS status;

-- ============================================
-- STEP 3: 설비 이동 이력 테이블 생성 (선택)
-- ============================================

CREATE TABLE IF NOT EXISTS equipment_move_logs (
  log_id INT AUTO_INCREMENT PRIMARY KEY,
  equipment_id INT UNSIGNED NOT NULL COMMENT '설비 ID',
  move_type ENUM('temporary', 'return') NOT NULL COMMENT '임시이동/복귀',
  from_workplace_id INT UNSIGNED COMMENT '이전 작업장',
  to_workplace_id INT UNSIGNED COMMENT '이동 작업장',
  from_x_percent DECIMAL(5,2) COMMENT '이전 X좌표',
  from_y_percent DECIMAL(5,2) COMMENT '이전 Y좌표',
  to_x_percent DECIMAL(5,2) COMMENT '이동 X좌표',
  to_y_percent DECIMAL(5,2) COMMENT '이동 Y좌표',
  reason TEXT COMMENT '이동 사유',
  moved_by INT COMMENT '이동 처리자',
  moved_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_eml_equipment FOREIGN KEY (equipment_id)
    REFERENCES equipments(equipment_id) ON DELETE CASCADE,
  CONSTRAINT fk_eml_from_workplace FOREIGN KEY (from_workplace_id)
    REFERENCES workplaces(workplace_id) ON DELETE SET NULL,
  CONSTRAINT fk_eml_to_workplace FOREIGN KEY (to_workplace_id)
    REFERENCES workplaces(workplace_id) ON DELETE SET NULL,
  CONSTRAINT fk_eml_moved_by FOREIGN KEY (moved_by)
    REFERENCES users(user_id) ON DELETE SET NULL,
  INDEX idx_eml_equipment_id (equipment_id),
  INDEX idx_eml_move_type (move_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SELECT 'equipment_move_logs 테이블 생성 완료' AS status;
