// 공차중계 운전자 API
import { apiClient } from "./client";

export interface DriverProfile {
  userId: string;
  userName: string;
  phoneNumber: string;
  licenseNumber: string;
  vehicleNumber: string;
  vehicleType: string | null;
  vehicleStatus: string | null;
  branchName: string | null;
}

export interface DriverProfileUpdateData {
  userName?: string;
  phoneNumber?: string;
  licenseNumber?: string;
  vehicleNumber?: string;
  vehicleType?: string;
  branchName?: string;
}

/**
 * 운전자 프로필 조회
 */
export async function getDriverProfile(): Promise<{
  success: boolean;
  data?: DriverProfile;
  message?: string;
}> {
  try {
    const response = await apiClient.get("/driver/profile");
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "프로필 조회에 실패했습니다.",
    };
  }
}

/**
 * 운전자 프로필 수정
 */
export async function updateDriverProfile(
  data: DriverProfileUpdateData
): Promise<{ success: boolean; message?: string }> {
  try {
    const response = await apiClient.put("/driver/profile", data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "프로필 수정에 실패했습니다.",
    };
  }
}

/**
 * 차량 상태 변경 (대기/정비)
 */
export async function updateDriverStatus(
  status: "off" | "maintenance"
): Promise<{ success: boolean; message?: string }> {
  try {
    const response = await apiClient.put("/driver/status", { status });
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "상태 변경에 실패했습니다.",
    };
  }
}

/**
 * 차량 삭제 (기록 보존)
 */
export async function deleteDriverVehicle(): Promise<{
  success: boolean;
  message?: string;
}> {
  try {
    const response = await apiClient.delete("/driver/vehicle");
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "차량 삭제에 실패했습니다.",
    };
  }
}

/**
 * 새 차량 등록
 */
export interface VehicleRegisterData {
  vehicleNumber: string;
  vehicleType?: string;
  branchName?: string;
}

export async function registerDriverVehicle(
  data: VehicleRegisterData
): Promise<{ success: boolean; message?: string }> {
  try {
    const response = await apiClient.post("/driver/vehicle", data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "차량 등록에 실패했습니다.",
    };
  }
}

/**
 * 회원 탈퇴
 */
export async function deleteDriverAccount(): Promise<{
  success: boolean;
  message?: string;
}> {
  try {
    const response = await apiClient.delete("/driver/account");
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || "회원 탈퇴에 실패했습니다.",
    };
  }
}

