package com.erp.service;

import com.erp.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Slf4j
public class DriverService extends BaseService {
    @Autowired private CommonService commonService;

    /** 운전자 목록 (admin용 / api_test 호환) */
    public Map<String, Object> getDriverList(Map<String, Object> params) {
        commonService.applyCompanyCodeFilter(params);
        commonService.applyPagination(params);
        Number cntNum = sqlSession.selectOne("driver.getDriverListCnt", params);
        int totalCount = cntNum != null ? cntNum.intValue() : 0;
        List<Map<String, Object>> list = sqlSession.selectList("driver.getDriverList", params);
        return commonService.buildListResponse(list, totalCount, params);
    }

    /** 운전자 프로필 조회 (user_info + vehicles 조합) */
    public Map<String, Object> getDriverProfile(Map<String, Object> params) {
        Map<String, Object> userInfo = sqlSession.selectOne("driver.getDriverUserInfo", params);
        if (userInfo == null) return null;

        Map<String, Object> vehicle = sqlSession.selectOne("driver.getDriverVehicle", params);

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("user_id", userInfo.get("user_id"));
        result.put("user_name", userInfo.get("user_name"));
        result.put("phone_number", userInfo.get("cell_phone"));
        result.put("license_number", userInfo.get("license_number"));
        result.put("vehicle_number", userInfo.get("vehicle_number"));
        result.put("vehicle_type", vehicle != null ? vehicle.get("vehicle_type") : null);
        result.put("vehicle_status", vehicle != null ? vehicle.get("status") : null);
        result.put("branch_name", userInfo.get("branch_name"));
        return result;
    }

    /** 프로필 수정 (user_info + vehicles 동시 수정) */
    @Transactional
    public void updateDriverProfile(Map<String, Object> params) {
        sqlSession.update("driver.updateDriverUserInfo", params);
        sqlSession.update("driver.updateDriverVehicle", params);
    }

    /** 차량 상태 변경 (off / maintenance) */
    @Transactional
    public void updateDriverStatus(Map<String, Object> params) {
        sqlSession.update("driver.updateDriverVehicleStatus", params);
    }

    /** 차량 삭제 (기록 보존: user_id=NULL, status=disabled) */
    @Transactional
    public void deleteDriverVehicle(Map<String, Object> params) {
        sqlSession.update("driver.softDeleteDriverVehicle", params);
        sqlSession.update("driver.clearDriverVehicleNumber", params);
    }

    /** 새 차량 등록 */
    @Transactional
    public void registerDriverVehicle(Map<String, Object> params) {
        Map<String, Object> userInfo = sqlSession.selectOne("driver.getDriverUserInfo", params);
        if (userInfo == null) {
            throw new IllegalArgumentException("사용자를 찾을 수 없습니다.");
        }
        // user_info에서 이름/전화번호 보완
        if (params.get("user_name") == null) params.put("user_name", userInfo.get("user_name"));
        if (params.get("phone_number") == null) params.put("phone_number", userInfo.get("cell_phone"));
        if (params.get("company_code") == null) params.put("company_code", userInfo.get("company_code"));

        sqlSession.insert("driver.insertDriverVehicle", params);

        // user_info.vehicle_number 업데이트
        Map<String, Object> updateParams = new HashMap<>();
        updateParams.put("user_id", params.get("user_id"));
        updateParams.put("vehicle_number", params.get("vehicle_number"));
        sqlSession.update("driver.updateDriverUserInfo", updateParams);
    }

    /** 회원 탈퇴 (vehicles + user_info 물리 삭제) */
    @Transactional
    public void deleteDriverAccount(Map<String, Object> params) {
        sqlSession.delete("driver.deleteDriverVehicles", params);
        sqlSession.delete("driver.deleteDriverUserInfo", params);
    }
}
