<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Request;
use App\Models\Unsubscribe;

final class UnsubscribeAdminController extends BaseController
{
    public function index(Request $request): void
    {
        $this->requireModule('unsubscribes');
        $page = $this->page();
        $source = (string) ($_GET['source'] ?? 'all');
        if (!in_array($source, ['all', 'campaign', 'manual', 'import'], true)) {
            $source = 'all';
        }
        $q = trim((string) ($_GET['q'] ?? ''));
        $result = Unsubscribe::paginate($page, 30, $source, $q);
        view('marketing/unsubscribes', [
            'title' => 'Unsubscribes',
            'rows' => $result['rows'],
            'total' => $result['total'],
            'page' => $page,
            'pages' => max(1, (int) ceil($result['total'] / 30)),
            'source' => $source,
            'q' => $q,
            'counts' => Unsubscribe::boardCounts(),
            'notifications' => $this->notifications(),
        ]);
    }
}
