<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

// =============================================================================
// Routes2.php - 파트너십 전용 라우팅 시스템 (완전 독립)
// 기존 Routes.php와 별개로 동작하는 파트너십 전용 라우팅
// =============================================================================

$routes->group('api', function($routes) {
    // =============================================================================
    // 벤더사-인플루언서 파트너십 관리 API (완전 재설계)
    // =============================================================================
    $routes->group('vendor-influencer', function($routes) {
        // 벤더사용 API
        $routes->post('requests', 'PartnershipController::getInfluencerRequests', [
            'as' => 'partnership.requests',
            'filter' => 'cors'
        ]); 
        $routes->post('approve', 'PartnershipController::processInfluencerRequest', [
            'as' => 'partnership.approve',
            'filter' => 'cors'
        ]); 
        $routes->post('terminate', 'PartnershipController::terminatePartnership', [
            'as' => 'partnership.terminate',
            'filter' => 'cors'
        ]); 
        
        // 인플루언서용 API
        $routes->post('search-vendors', 'PartnershipController::searchVendorsForInfluencer', [
            'as' => 'partnership.search_vendors',
            'filter' => 'cors'
        ]); 
        $routes->post('create-request', 'PartnershipController::createPartnershipRequest', [
            'as' => 'partnership.create_request',
            'filter' => 'cors'
        ]); 
        $routes->post('reapply-request', 'PartnershipController::createReapplyRequest', [
            'as' => 'partnership.reapply_request',
            'filter' => 'cors'
        ]); 
        
        // 공용 API
        $routes->post('list', 'PartnershipController::getPartnershipList', [
            'as' => 'partnership.list',
            'filter' => 'cors'
        ]);
        $routes->post('detail/(:num)', 'PartnershipController::getPartnershipDetail/$1', [
            'as' => 'partnership.detail',
            'filter' => 'cors'
        ]);
        $routes->post('history/(:num)', 'PartnershipController::getPartnershipHistory/$1', [
            'as' => 'partnership.history',
            'filter' => 'cors'
        ]);
        $routes->post('stats/(:num)', 'PartnershipController::getPartnershipStats/$1', [
            'as' => 'partnership.stats',
            'filter' => 'cors'
        ]);
        
        // 관리용 API (개발/디버깅)
        $routes->get('debug/partnership/(:num)', 'PartnershipController::debugPartnership/$1', [
            'as' => 'partnership.debug',
            'filter' => 'cors'
        ]);
        $routes->get('debug/stats/(:num)', 'PartnershipController::getPartnershipStats/$1', [
            'as' => 'partnership.debug_stats',
            'filter' => 'cors'
        ]);
        $routes->get('debug/all', 'PartnershipController::debugAllPartnerships', [
            'as' => 'partnership.debug_all',
            'filter' => 'cors'
        ]);
    });

    // =============================================================================
    // 파트너십 대시보드 API (향후 확장)
    // =============================================================================
    $routes->group('partnership-dashboard', function($routes) {
        $routes->post('vendor/(:num)', 'PartnershipDashboardController::getVendorDashboard/$1', [
            'as' => 'partnership_dashboard.vendor',
            'filter' => 'cors'
        ]);
        $routes->post('influencer/(:num)', 'PartnershipDashboardController::getInfluencerDashboard/$1', [
            'as' => 'partnership_dashboard.influencer', 
            'filter' => 'cors'
        ]);
        $routes->post('analytics', 'PartnershipDashboardController::getAnalytics', [
            'as' => 'partnership_dashboard.analytics',
            'filter' => 'cors'
        ]);
    });

    // =============================================================================
    // 파트너십 알림 API (향후 확장)
    // =============================================================================
    $routes->group('partnership-notifications', function($routes) {
        $routes->post('send', 'PartnershipNotificationController::sendNotification', [
            'as' => 'partnership_notifications.send',
            'filter' => 'cors'
        ]);
        $routes->post('list/(:num)', 'PartnershipNotificationController::getNotifications/$1', [
            'as' => 'partnership_notifications.list',
            'filter' => 'cors'
        ]);
        $routes->post('mark-read', 'PartnershipNotificationController::markAsRead', [
            'as' => 'partnership_notifications.mark_read',
            'filter' => 'cors'
        ]);
    });
});

// =============================================================================
// 배송 관리 라우트 (api 그룹 외부)
// =============================================================================
$routes->post('deli/updateDeliveryInfo', 'Deli::updateDeliveryInfo');
$routes->post('deli/shipping', 'Deli::getShippingList');
$routes->post('deli/delivered', 'Deli::getDeliveredList');
$routes->post('deli/markDelivered', 'Deli::markAsDelivered');
$routes->post('deli/markSettled', 'Deli::markAsSettled');
$routes->post('deli/settlement', 'Deli::getSettlementList');
$routes->post('deli/pendingOrdersForVendor', 'Deli::getPendingOrdersForVendor');

// =============================================================================
// 파트너십 전용 웹 라우팅 (향후 확장)
// =============================================================================

$routes->group('partnership', function($routes) {
    // 파트너십 전용 대시보드 페이지 (향후 구현)
    $routes->get('dashboard/vendor/(:num)', 'PartnershipWebController::vendorDashboard/$1', [
        'as' => 'partnership_web.vendor_dashboard'
    ]);
    $routes->get('dashboard/influencer/(:num)', 'PartnershipWebController::influencerDashboard/$1', [
        'as' => 'partnership_web.influencer_dashboard'  
    ]);
    $routes->get('analytics', 'PartnershipWebController::analytics', [
        'as' => 'partnership_web.analytics'
    ]);
    
    // 파트너십 관리 페이지
    $routes->get('manage', 'PartnershipWebController::manage', [
        'as' => 'partnership_web.manage'
    ]);
    $routes->get('settings', 'PartnershipWebController::settings', [
        'as' => 'partnership_web.settings'
    ]);
});

// =============================================================================
// 파트너십 시스템 헬스체크 및 문서
// =============================================================================

$routes->group('health', function($routes) {
    // 파트너십 시스템 헬스체크
    $routes->get('partnership', function() {
        return service('response')->setJSON([
            'status' => 'OK',
            'message' => 'Partnership System (Routes2.php) is healthy',
            'timestamp' => date('Y-m-d H:i:s'),
            'version' => '2.0',
            'system' => 'Routes2.php - Independent Partnership System',
            'api_endpoints' => [
                'vendor_apis' => [
                    'POST /api/vendor-influencer/requests',
                    'POST /api/vendor-influencer/approve', 
                    'POST /api/vendor-influencer/terminate'
                ],
                'influencer_apis' => [
                    'POST /api/vendor-influencer/search-vendors',
                    'POST /api/vendor-influencer/create-request',
                    'POST /api/vendor-influencer/reapply-request'
                ],
                'common_apis' => [
                    'POST /api/vendor-influencer/list',
                    'POST /api/vendor-influencer/detail/{id}',
                    'POST /api/vendor-influencer/history/{id}',
                    'POST /api/vendor-influencer/stats/{id}'
                ],
                'debug_apis' => [
                    'GET /api/vendor-influencer/debug/partnership/{id}',
                    'GET /api/vendor-influencer/debug/stats/{id}',
                    'GET /api/vendor-influencer/debug/all'
                ]
            ],
            'database' => [
                'table' => 'VENDOR_INFLUENCER_PARTNERSHIP',
                'model' => 'VendorInfluencerPartnershipModel',
                'controller' => 'PartnershipController'
            ]
        ]);
    }, ['as' => 'health.partnership']);

    // 파트너십 API 문서
    $routes->get('partnership/docs', function() {
        return service('response')->setJSON([
            'title' => 'Partnership API Documentation (Routes2.php)',
            'description' => '벤더사-인플루언서 파트너십 관리 API 문서',
            'version' => '2.0',
            'base_url' => base_url('api/vendor-influencer'),
            'authentication' => 'Bearer Token (JWT)',
            'endpoints' => [
                [
                    'method' => 'POST',
                    'path' => '/requests',
                    'description' => '벤더사의 인플루언서 요청 목록 조회',
                    'parameters' => [
                        'vendorSeq' => 'integer (required)',
                        'status' => 'string (optional)',
                        'keyword' => 'string (optional)',
                        'page' => 'integer (optional)',
                        'size' => 'integer (optional)'
                    ]
                ],
                [
                    'method' => 'POST',
                    'path' => '/approve',
                    'description' => '파트너십 승인/거부 처리',
                    'parameters' => [
                        'mappingSeq' => 'integer (required)',
                        'action' => 'string (required) - APPROVE|REJECT',
                        'processedBy' => 'integer (required)',
                        'responseMessage' => 'string (optional)'
                    ]
                ],
                [
                    'method' => 'POST',
                    'path' => '/terminate',
                    'description' => '파트너십 해지 처리',
                    'parameters' => [
                        'mappingSeq' => 'integer (required)',
                        'terminatedBy' => 'integer (required)',
                        'terminateReason' => 'string (optional)'
                    ]
                ],
                [
                    'method' => 'POST',
                    'path' => '/search-vendors',
                    'description' => '인플루언서의 벤더사 검색',
                    'parameters' => [
                        'influencerSeq' => 'integer (required)',
                        'keyword' => 'string (optional)',
                        'category' => 'string (optional)',
                        'page' => 'integer (optional)',
                        'size' => 'integer (optional)'
                    ]
                ],
                [
                    'method' => 'POST',
                    'path' => '/create-request',
                    'description' => '파트너십 요청 생성',
                    'parameters' => [
                        'vendorSeq' => 'integer (required)',
                        'influencerSeq' => 'integer (required)',
                        'requestMessage' => 'string (optional)',
                        'commissionRate' => 'decimal (optional)',
                        'specialConditions' => 'string (optional)'
                    ]
                ],
                [
                    'method' => 'POST',
                    'path' => '/reapply-request',
                    'description' => '재승인 요청 생성',
                    'parameters' => [
                        'vendorSeq' => 'integer (required)',
                        'influencerSeq' => 'integer (required)',
                        'requestMessage' => 'string (optional)',
                        'commissionRate' => 'decimal (optional)',
                        'specialConditions' => 'string (optional)'
                    ]
                ]
            ]
        ]);
    }, ['as' => 'health.partnership_docs']);
});

// =============================================================================
// Routes2.php 정보 및 관리
// =============================================================================

$routes->get('routes2', function() {
    return service('response')->setJSON([
        'title' => 'Routes2.php - Independent Partnership Routing System',
        'description' => '기존 Routes.php와 독립적으로 운영되는 파트너십 전용 라우팅 시스템',
        'version' => '2.0',
        'created' => '2024-12-22',
        'features' => [
            '완전 독립적인 파트너십 라우팅',
            '기존 시스템과 충돌 없음',
            '확장 가능한 구조',
            '개발자 친화적 디버깅 도구',
            '자동 API 문서화'
        ],
        'routes_count' => [
            'api_routes' => 12,
            'web_routes' => 5,
            'health_routes' => 2,
            'debug_routes' => 3
        ],
        'health_check' => base_url('health/partnership'),
        'api_docs' => base_url('health/partnership/docs'),
        'status' => 'Active and Independent'
    ]);
}, ['as' => 'routes2.info']); 