import axios from 'axios';
import { config } from './config';

const API_BASE_URL = '/api';

export const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 30000,
  headers: { 'Content-Type': 'application/json' },
});

// SSO 쿠키에서 토큰 읽기
function getSSOToken() {
  const match = document.cookie.match(/sso_token=([^;]*)/);
  return match ? match[1] : null;
}

// 요청 인터셉터: SSO 토큰 자동 추가
api.interceptors.request.use(config => {
  const token = getSSOToken();
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// 응답 인터셉터: 401 시 SSO 로그인 리다이렉트
api.interceptors.response.use(
  response => response,
  error => {
    if (error.response?.status === 401) {
      window.location.href = config.ssoLoginUrl(window.location.href);
      return new Promise(() => {}); // 리다이렉트 중 pending
    }
    return Promise.reject(error);
  }
);

// 파일 업로드
export function uploadFile(formData, options = {}) {
  return api.post('/files/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    ...options,
  });
}

export function fetchMaterials(params) { return api.get('/files/materials-v2', { params }); }
export function fetchMaterialsSummary(params) { return api.get('/files/materials/summary', { params }); }
export function fetchFiles(params) { return api.get('/files', { params }); }
export function deleteFile(fileId) { return api.delete(`/files/delete/${fileId}`); }
export function fetchJobs(params) { return api.get('/jobs/', { params }); }
export function createJob(data) { return api.post('/jobs/', data); }

export function compareRevisions(jobNo, filename, oldRevision, newRevision) {
  return api.get('/files/materials/compare-revisions', {
    params: { job_no: jobNo, filename, old_revision: oldRevision, new_revision: newRevision }
  });
}

export function compareMaterialRevisions(jobNo, currentRevision, previousRevision = null, saveResult = true) {
  return api.post('/materials/compare-revisions', null, {
    params: { job_no: jobNo, current_revision: currentRevision, previous_revision: previousRevision, save_result: saveResult }
  });
}

export function getMaterialComparisonHistory(jobNo, limit = 10) {
  return api.get('/materials/comparison-history', { params: { job_no: jobNo, limit } });
}

export function getMaterialInventoryStatus(jobNo, materialHash = null) {
  return api.get('/materials/inventory-status', { params: { job_no: jobNo, material_hash: materialHash } });
}

export function confirmMaterialPurchase(jobNo, revision, confirmations, confirmedBy = 'user') {
  return api.post('/materials/confirm-purchase', confirmations, {
    params: { job_no: jobNo, revision, confirmed_by: confirmedBy }
  });
}

export function getMaterialPurchaseStatus(jobNo, revision = null, status = null) {
  return api.get('/materials/purchase-status', { params: { job_no: jobNo, revision, status } });
}

export default api;
