<?php
  
  use CodeIgniter\Router\RouteCollection;
  
  /**
   * @var RouteCollection $routes
   */
  $routes->get('auth/googleLogin', 'Auth::googleLogin');
  $routes->get('auth/callback', 'Auth::callback');
  $routes->post('auth/joinmember', 'Auth::join');
  $routes->post('auth/joinvendor', 'Auth::joinVendor');
  $routes->post('auth/withdrawal', 'Auth::withdrawal'); //구글 회원탈퇴 , 일반회원 탈퇴
  $routes->post('auth/kakaowithdrawal', 'Auth::kakaoWithdrawal'); //카카오 회웥탈퇴
  $routes->get('auth/kakaoLogin', 'Auth::kakaoLogin');
  $routes->get('auth/kakao', 'Auth::kakao');
  $routes->get('auth/naverLogin', 'Auth::naverLogin');
  $routes->get('auth/naver', 'Auth::naver');
  $routes->get('auth/naverWithdrawal', 'Auth::naverWithdrawal');
  $routes->post('auth/checkId', 'Auth::checkId'); // 사용 중 체크 아이디
  
  $routes->get('/', 'Home::index'); //홈화면 리다이렉트용
  $routes->post('roulette/login', 'Roulette::login'); //로그인 페이지 토큰 상관없이 호출가능
  $routes->post('roulette/refreshToken', 'Roulette::refreshToken'); //엑세스 토큰이 있어야만 발급 가능
  
  $routes->get('alimtalk/send', 'Alimtalk::send');
  $routes->post('alimtalk/send', 'Alimtalk::send'); // POST 요청인 경우
  
  $routes->post('winner/reg', 'Winner::winnerReg');
  $routes->post('winner/itemcount', 'Winner::itemCount');
  $routes->post('winner/winnerchk', 'Winner::winnerChk');

// 관리자 라우트
  $routes->post('mng/list', 'Mng::mnglist');
  $routes->post('mng/search', 'Mng::mngSearch');
  $routes->post('mng/reg', 'Mng::mngRegister');
  $routes->post('mng/chk', 'Mng::mngIDChk');
  $routes->post('mng/update', 'Mng::mngUpdate');
  $routes->get('mng/detail/(:segment)', 'Mng::mngDetail/$1');
  $routes->post('mng/stupdate/(:segment)', 'Mng::mngStatusUpdate/$1');
  $routes->post('mng/delete/(:segment)', 'Mng::mngDelete/$1');

// 아이템 라우트
  $routes->post('item/list', 'Item::itemlist');
  $routes->post('item/reg', 'Item::itemRegister');
  $routes->get('item/detail/(:num)', 'Item::itemDetail/$1');
  $routes->post('item/update/(:num)', 'Item::itemUpdate/$1');
  $routes->post('item/delete/(:num)', 'Item::itemDelete/$1');
  $routes->post('item/search', 'Item::itemSearch');
// 파일 다운로드
  $routes->get('item/download/(:segment)', 'Item::file/$1');

// 제품 주문 라우트
  $routes->post('deli/itemlist', 'Deli::itemlist');
  $routes->post('deli/list', 'Deli::delilist');
  $routes->post('deli/reg', 'Deli::deliRegister');

// 당첨자 라우트
  $routes->post('winner/list', 'Winner::winnerlist');
  $routes->get('winner/detail/(:num)', 'Winner::winnerDetail/$1');
  $routes->post('winner/partclist', 'Winner::getParticipationByItem');
  $routes->post('winner/matcheduser', 'Winner::matchedUser');
  
  $routes->group('', ['filter' => 'auth'], function ($routes) {
  });

// API 라우트 그룹
  $routes->group('api', ['namespace' => 'App\Controllers'], function($routes) {
    
    // 벤더사 관련 API
    $routes->group('vendor', function($routes) {
      $routes->post('search', 'InfluencerController::searchVendors'); // 기존 VendorInfluencerController → InfluencerController
      $routes->post('list', 'VendorController::getList');
      $routes->post('detail', 'VendorController::getDetail');
      $routes->post('create', 'VendorController::create');
      $routes->post('update', 'VendorController::update');
      $routes->post('delete', 'VendorController::delete');
    });
    
    // 벤더사-인플루언서 매핑 관련 API는 Routes2.php에서 관리
    // (기존 파트너십 라우팅은 별도 파일로 분리됨)
    
    // 인증 관련 API
    $routes->group('auth', function($routes) {
      $routes->post('login', 'AuthController::login');
      $routes->post('logout', 'AuthController::logout');
      $routes->post('register', 'AuthController::register');
      $routes->post('refresh', 'AuthController::refreshToken');
      $routes->post('verify', 'AuthController::verifyToken');
    });
    
    // 사용자 관련 API
    $routes->group('user', function($routes) {
      $routes->post('profile', 'UserController::getProfile');
      $routes->post('update-profile', 'UserController::updateProfile');
      $routes->post('change-password', 'UserController::changePassword');
      $routes->post('upload-avatar', 'UserController::uploadAvatar');
    });
    
    // 인플루언서 관련 API (새로 추가)
    $routes->group('influencer', function($routes) {
        $routes->post('search-vendors', 'InfluencerController::searchVendors');
        $routes->post('create-request', 'InfluencerController::createApprovalRequest');
        $routes->post('reapply-request', 'InfluencerController::createReapplyRequest');
        $routes->post('my-partnerships', 'InfluencerController::getMyPartnerships');
        $routes->post('terminate', 'InfluencerController::terminatePartnership');
        $routes->post('profile', 'InfluencerController::getProfile');
    });
    
    // 제품 관련 API
    $routes->group('item', function($routes) {
      $routes->post('list', 'ItemController::getList');
      $routes->post('detail', 'ItemController::getDetail');
      $routes->post('create', 'ItemController::create');
      $routes->post('update', 'ItemController::update');
      $routes->post('delete', 'ItemController::delete');
      $routes->post('search', 'ItemController::search');
    });
    
    // 파일 업로드 관련 API
    $routes->group('upload', function($routes) {
      $routes->post('image', 'UploadController::uploadImage');
      $routes->post('file', 'UploadController::uploadFile');
      $routes->post('multiple', 'UploadController::uploadMultiple');
    });
    
    // 알림 관련 API
    $routes->group('notification', function($routes) {
      $routes->post('list', 'NotificationController::getList');
      $routes->post('mark-read', 'NotificationController::markAsRead');
      $routes->post('mark-all-read', 'NotificationController::markAllAsRead');
      $routes->post('delete', 'NotificationController::delete');
    });
    
    // 대시보드 관련 API
    $routes->group('dashboard', function($routes) {
      $routes->post('stats', 'DashboardController::getStats');
      $routes->post('recent-activities', 'DashboardController::getRecentActivities');
      $routes->post('chart-data', 'DashboardController::getChartData');
    });
  });

// 인증이 필요한 API 라우트 (필터 적용)
  $routes->group('api', ['namespace' => 'App\Controllers', 'filter' => 'auth'], function($routes) {
    
    // 보호된 벤더사-인플루언서 API (VendorInfluencerController → 새 컨트롤러들로 교체)
    $routes->group('vendor-influencer/protected', function($routes) {
      $routes->post('my-requests', 'InfluencerController::getMyRequests');
      $routes->post('my-partnerships', 'InfluencerController::getMyPartnerships');
      $routes->post('pending-approvals', 'VendorController::getPendingApprovals');
    });
    
    // 관리자 전용 API
    $routes->group('admin', ['filter' => 'admin'], function($routes) {
      $routes->post('vendor-influencer/all', 'AdminController::getAllMappings');
      $routes->post('vendor-influencer/expired', 'AdminController::getExpiredRequests');
      $routes->post('vendor-influencer/process-expired', 'AdminController::processExpiredRequests');
      $routes->post('system/stats', 'AdminController::getSystemStats');
    });
  });

// 웹훅 및 외부 API
  $routes->group('webhook', ['namespace' => 'App\Controllers'], function($routes) {
    $routes->post('payment/success', 'WebhookController::paymentSuccess');
    $routes->post('payment/failure', 'WebhookController::paymentFailure');
    $routes->post('notification/send', 'WebhookController::sendNotification');
  });

// 크론잡 및 스케줄러 API
  $routes->group('cron', ['namespace' => 'App\Controllers', 'filter' => 'cron'], function($routes) {
    $routes->get('process-expired-requests', 'CronController::processExpiredRequests');
    $routes->get('send-reminder-notifications', 'CronController::sendReminderNotifications');
    $routes->get('cleanup-old-data', 'CronController::cleanupOldData');
  });

// 개발 및 테스트용 라우트 (개발 환경에서만 사용)
  if (ENVIRONMENT === 'development') {
    $routes->group('dev', ['namespace' => 'App\Controllers'], function($routes) {
      $routes->get('test-db', 'DevController::testDatabase');
      $routes->get('seed-data', 'DevController::seedTestData');
      $routes->get('clear-cache', 'DevController::clearCache');
      $routes->post('test-api', 'DevController::testApi');
    });
  }

// 디버깅용 라우트 (임시)
  $routes->group('debug', ['namespace' => 'App\\Controllers'], function($routes) {
    $routes->get('foreign-key', 'DebugController::debugForeignKey');
    $routes->get('simple-update', 'DebugController::testSimpleUpdate');
  });

// =============================================================================
// 추가 라우팅 파일 로드
// =============================================================================

// Routes2.php 파일 로드 (파트너십 관련 라우팅)
if (file_exists(APPPATH . 'Config/Routes2.php')) {
    require_once APPPATH . 'Config/Routes2.php';
}

// 인플루언서 요청 라우트 (기존 구조와 호환성)
  $routes->group('influencer-request', function($routes) {
    $routes->post('create', 'InfluencerController::createApprovalRequest');
    $routes->post('vendor-approval', 'VendorController::processInfluencerRequest'); // 벤더사의 인플루언서 승인/거절
    $routes->post('search-vendors', 'InfluencerController::searchVendors'); // 인플루언서의 벤더사 검색
    $routes->post('get-list', 'InfluencerController::getMyPartnerships'); // 인플루언서의 파트너십 목록
    $routes->post('get-vendor-list', 'VendorController::getInfluencerRequests'); // 벤더사의 요청 목록
    $routes->post('terminate', 'InfluencerController::terminatePartnership'); // 인플루언서의 파트너십 해지
    $routes->post('vendor-terminate', 'VendorController::terminatePartnership'); // 벤더사의 파트너십 해지
    $routes->post('status-stats', 'VendorController::getStatusStats'); // 벤더사 요청 통계
    $routes->post('reapply-request', 'InfluencerController::createReapplyRequest'); // 인플루언서 재승인 요청
  });
  
  $routes->post('api/influencer/profile', 'InfluencerController::getProfile');

    // 디버깅 라우트 (개발용)
    $routes->get('debug/mapping/(:num)', 'VendorController::debugMappingStatus/$1');
    $routes->get('debug/mapping', 'VendorController::debugMappingStatus');
    $routes->get('debug/sync-mapping', 'VendorController::syncMappingStatus');
    $routes->post('debug/history-insert', 'VendorController::debugHistoryInsert');

