<?php
namespace Crawl\Controllers;

use Crawl\Lib\View;
use Crawl\Repositories\ProjectRepository;

class WebController
{
    public function auditPage(): void
    {
        $projects = (new ProjectRepository())->all();

        View::render('audit/index', [
            'pageTitle' => 'Website Audit',
            'projects'  => $projects,
        ]);
    }

    public function projectsPage(): void
    {
        View::render('projects/index', [
            'pageTitle' => 'Health Board',
        ]);
    }

    public function reportPage(): void
    {
        $projectId = (int)($_GET['p']   ?? 0);
        $runId     = (int)($_GET['run'] ?? 0);
        $project   = $projectId ? (new ProjectRepository())->find($projectId) : null;

        View::render('report/index', [
            'pageTitle' => 'Audit Report' . ($project ? ' — ' . $project['business_name'] : ''),
            'projectId' => $projectId,
            'runId'     => $runId,
            'project'   => $project,
        ]);
    }

    public function clientReportPage(): void
    {
        $projectId = (int)($_GET['p']   ?? 0);
        $runId     = (int)($_GET['run'] ?? 0);
        $project   = $projectId ? (new ProjectRepository())->find($projectId) : null;

        // Pull agency + commercial config from .env
        $agency = [
            'name'     => $_ENV['AGENCY_NAME']    ?? 'Your Agency',
            'tagline'  => $_ENV['AGENCY_TAGLINE'] ?? '',
            'email'    => $_ENV['AGENCY_EMAIL']   ?? '',
            'phone'    => $_ENV['AGENCY_PHONE']   ?? '',
            'website'  => $_ENV['AGENCY_WEBSITE'] ?? '',
            'logo_url' => $_ENV['AGENCY_LOGO_URL'] ?? '',
            'accent'   => $_ENV['REPORT_ACCENT_COLOR'] ?? '#2563eb',
        ];

        $commercialsEnabled = filter_var($_ENV['COMMERCIALS_ENABLED'] ?? true, FILTER_VALIDATE_BOOL);
        $packages = [];
        for ($i = 1; $i <= 3; $i++) {
            $name = $_ENV["SEO_PKG{$i}_NAME"] ?? '';
            if ($name) {
                $packages[] = [
                    'name'     => $name,
                    'type'     => $_ENV["SEO_PKG{$i}_TYPE"]     ?? 'Monthly',
                    'price'    => $_ENV["SEO_PKG{$i}_PRICE"]    ?? '',
                    'currency' => $_ENV["SEO_PKG{$i}_CURRENCY"] ?? 'INR',
                    'desc'     => $_ENV["SEO_PKG{$i}_DESC"]     ?? '',
                    'includes' => $_ENV["SEO_PKG{$i}_INCLUDES"] ?? '',
                ];
            }
        }

        View::render('report/client', [
            'pageTitle'           => 'Website Audit Report' . ($project ? ' — ' . $project['business_name'] : ''),
            'projectId'           => $projectId,
            'runId'               => $runId,
            'project'             => $project,
            'agency'              => $agency,
            'packages'            => $packages,
            'commercialsEnabled'  => $commercialsEnabled,
        ]);
    }
}
