<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

// ----------------------------------------------------------------
// API Routes
// ----------------------------------------------------------------

$routes->group('api', ['namespace' => 'App\Controllers\Api'], function ($routes) {

    // CORS preflight — no auth
    $routes->options('(:any)', static function () {
        return response()->setStatusCode(200);
    });

    // Auth — public
    $routes->group('auth', function ($routes) {
        $routes->post('login', 'AuthController::login');
        $routes->post('logout',  'AuthController::logout',  ['filter' => 'auth']);
        $routes->get('me',       'AuthController::me',      ['filter' => 'auth']);
        $routes->post('refresh', 'AuthController::refresh', ['filter' => 'auth']);
    });

    // Churches — platform admin only
    $routes->group('churches', ['filter' => 'auth'], function ($routes) {
        $routes->get('',                        'ChurchController::index');
        $routes->post('',                       'ChurchController::create');
        $routes->get('(:segment)',              'ChurchController::show/$1');
        $routes->put('(:segment)',              'ChurchController::update/$1');
        $routes->get('(:segment)/branches',    'ChurchController::branches/$1');
    });

    // Protected routes — require auth + church scope
    $routes->group('', ['filter' => ['auth', 'churchscope']], function ($routes) {

        // Users
        $routes->group('users', function ($routes) {
            $routes->get('',                            'UserController::index');
            $routes->post('',                           'UserController::create');
            $routes->get('(:segment)',                  'UserController::show/$1');
            $routes->put('(:segment)',                  'UserController::update/$1');
            $routes->delete('(:segment)',               'UserController::delete/$1');
            $routes->put('(:segment)/permissions',      'UserController::setPermissions/$1');
            $routes->post('(:segment)/reset-password',  'UserController::resetPassword/$1');
        });

        // Members
        $routes->group('members', function ($routes) {
            $routes->get('stats',          'MemberController::stats');
            $routes->get('',               'MemberController::index');
            $routes->post('',              'MemberController::create');
            $routes->get('(:segment)',     'MemberController::show/$1');
            $routes->put('(:segment)',     'MemberController::update/$1');
            $routes->delete('(:segment)', 'MemberController::delete/$1');
        });

        // Finance
        $routes->group('finance', function ($routes) {
            $routes->get('transactions',               'FinanceController::index');
            $routes->post('transactions',              'FinanceController::create');
            $routes->get('transactions/(:segment)',    'FinanceController::show/$1');
            $routes->put('transactions/(:segment)',    'FinanceController::update/$1');
            $routes->delete('transactions/(:segment)', 'FinanceController::delete/$1');
            $routes->get('summary',                    'FinanceController::summary');
            $routes->post('attachments',               'FinanceController::uploadAttachment');
            $routes->get('categories',                 'FinanceController::categories');
        });

        // Prayer
        $routes->group('prayer', function ($routes) {
            $routes->get('',                           'PrayerController::index');
            $routes->post('',                          'PrayerController::create');
            $routes->get('(:segment)',                 'PrayerController::show/$1');
            $routes->put('(:segment)',                 'PrayerController::update/$1');
            $routes->delete('(:segment)',              'PrayerController::delete/$1');
            $routes->post('(:segment)/respond',        'PrayerController::respond/$1');
            $routes->put('(:segment)/status',          'PrayerController::updateStatus/$1');
        });

        // Committee
        $routes->group('committee', function ($routes) {
            $routes->get('',               'CommitteeController::index');
            $routes->post('',              'CommitteeController::create');
            $routes->put('(:segment)',     'CommitteeController::update/$1');
            $routes->delete('(:segment)', 'CommitteeController::delete/$1');
        });

        // Events
        $routes->group('events', function ($routes) {
            $routes->get('',                          'EventController::index');
            $routes->post('',                         'EventController::create');
            $routes->get('(:segment)',                'EventController::show/$1');
            $routes->put('(:segment)',                'EventController::update/$1');
            $routes->delete('(:segment)',             'EventController::delete/$1');
            $routes->post('(:segment)/attend',        'EventController::attend/$1');
            $routes->get('(:segment)/attendance',     'EventController::attendance/$1');
        });

        // Families
        $routes->group('families', function ($routes) {
            $routes->get('',                                         'FamilyController::index');
            $routes->post('',                                        'FamilyController::create');
            $routes->get('(:segment)',                               'FamilyController::show/$1');
            $routes->put('(:segment)',                               'FamilyController::update/$1');
            $routes->delete('(:segment)',                            'FamilyController::delete/$1');
            $routes->post('(:segment)/members',                      'FamilyController::addMember/$1');
            $routes->delete('(:segment)/members/(:segment)',         'FamilyController::removeMember/$1/$2');
        });

        // Donations
        $routes->group('donations', function ($routes) {
            $routes->get('',                               'DonationController::index');
            $routes->post('',                              'DonationController::create');
            $routes->get('(:segment)',                     'DonationController::show/$1');
            $routes->put('(:segment)',                     'DonationController::update/$1');
            $routes->delete('(:segment)',                  'DonationController::delete/$1');
            $routes->post('(:segment)/contribute',         'DonationController::contribute/$1');
            $routes->get('(:segment)/contributions',       'DonationController::contributions/$1');
        });

        // Settings
        $routes->group('settings', function ($routes) {
            $routes->get('',                               'SettingController::index');
            $routes->put('',                               'SettingController::update');
            $routes->get('roles',                          'SettingController::roles');
            $routes->put('roles/(:num)/permissions',       'SettingController::updateRolePermissions/$1');
            $routes->get('categories',                     'SettingController::categories');
            $routes->put('categories',                     'SettingController::updateCategories');
        });

        // Reports
        $routes->group('reports', function ($routes) {
            $routes->get('finance',     'ReportController::finance');
            $routes->get('members',     'ReportController::members');
            $routes->get('attendance',  'ReportController::attendance');
            $routes->get('donations',   'ReportController::donations');
        });

        // Audit Logs
        $routes->get('audit-logs', 'AuditLogController::index');
    });
});
