<?hh // strict

/**
 * Main application entry point for MerchantsOfHope
 */

require_once __DIR__ . '/bootstrap.php';

use App\Controllers\HomeController;
use App\Middleware\TenantMiddleware;
use App\Services\TenantResolver;
use Slim\Factory\AppFactory;
use Slim\Middleware\ContentLengthMiddleware;
use Slim\Routing\RouteCollectorProxy;

// Set up the Slim application
AppFactory::setContainer($container);
$app = AppFactory::create();

// Add middleware
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->add(new ContentLengthMiddleware());

// Add tenant middleware
$tenantResolver = new TenantResolver();
$tenantMiddleware = new TenantMiddleware($tenantResolver);
$app->add($tenantMiddleware);

// Define routes
$app->get('/', [HomeController::class, 'index']);

// Social login routes (not under API group to avoid tenant isolation)
$app->get('/auth/google/callback', [App\Controllers\AuthController::class, 'googleCallback']);
$app->get('/auth/github/callback', [App\Controllers\AuthController::class, 'githubCallback']);

// Group routes for API
$app->group('/api', function (RouteCollectorProxy $group) {
    // Authentication routes
    $group->post('/auth/login', [App\Controllers\AuthController::class, 'login']);
    $group->post('/auth/logout', [App\Controllers\AuthController::class, 'logout']);
    $group->post('/auth/register', [App\Controllers\AuthController::class, 'register']);
    
    // Job seeker routes
    $group->get('/jobs', [App\Controllers\JobController::class, 'listJobs']);
    $group->get('/jobs/{id}', [App\Controllers\JobController::class, 'getJob']);
    $group->post('/applications', [App\Controllers\ApplicationController::class, 'apply']);
    $group->get('/my-applications', [App\Controllers\ApplicationController::class, 'getApplicationsByUser']);
    
    // Job provider routes
    $group->get('/my-jobs', [App\Controllers\JobController::class, 'myJobs']);
    $group->post('/jobs', [App\Controllers\JobController::class, 'createJob']);
    $group->put('/jobs/{id}', [App\Controllers\JobController::class, 'updateJob']);
    $group->delete('/jobs/{id}', [App\Controllers\JobController::class, 'deleteJob']);
    $group->get('/jobs/{id}/applications', [App\Controllers\ApplicationController::class, 'getApplicationsByJob']);
});

// Add error middleware in development
if (APP_ENV === 'development') {
    $app->addErrorMiddleware(true, true, true);
} else {
    $app->addErrorMiddleware(false, false, false);
}

// Run the application
$app->run();