<?php
class OnboardingController {
    public function showBusiness(): void {
        Auth::requireLogin();
        include APP_ROOT . '/views/public/onboarding/business.php';
    }

    public function saveBusiness(): void {
        Auth::requireLogin();
        Security::checkCsrf();
        $tenantId = Auth::tenantId();
        $data = [
            'business_name'    => Security::sanitize($_POST['business_name'] ?? ''),
            'business_address' => Security::sanitize($_POST['business_address'] ?? ''),
            'business_phone'   => Security::sanitize($_POST['business_phone'] ?? ''),
            'industry'         => Security::sanitize($_POST['industry'] ?? ''),
        ];
        TenantModel::update($tenantId, $data);
        // Update session
        $tenant = TenantModel::findById($tenantId);
        $_SESSION['tenant'] = $tenant;
        redirect('onboarding/plan');
    }

    public function showPlan(): void {
        Auth::requireLogin();
        $plans = PlanModel::all();
        $current = PlanModel::getActiveSubscription(Auth::tenantId());
        include APP_ROOT . '/views/public/onboarding/plan.php';
    }

    public function selectPlan(): void {
        Auth::requireLogin();
        Security::checkCsrf();
        $planId = (int)($_POST['plan_id'] ?? 0);
        $cycle  = ($_POST['billing_cycle'] ?? 'monthly') === 'yearly' ? 'yearly' : 'monthly';
        $plan   = PlanModel::find($planId);
        if (!$plan) { flash('error','Invalid plan selected.'); redirect('onboarding/plan'); }
        PlanModel::subscribe(Auth::tenantId(), $planId, $cycle);
        redirect('onboarding/done');
    }

    public function done(): void {
        Auth::requireLogin();
        include APP_ROOT . '/views/public/onboarding/done.php';
    }
}
