<?php

namespace App\SiteReadiness\Checks;

use App\SiteReadiness\CheckResult;
use App\SiteReadiness\ReadinessCheck;
use App\SiteReadiness\Status;

class MailConfiguredCheck implements ReadinessCheck
{
    public function group(): string { return 'Mail'; }
    public function label(): string { return 'Mailgun configured'; }
    public function hint(): string { return 'Enable Mailgun (MAILGUN_ENABLED) and set its SMTP credentials for production.'; }

    public function run(): CheckResult
    {
        $prod = config('app.env') === 'production';
        $enabled = defined('MAILGUN_ENABLED') && \MAILGUN_ENABLED;

        if (!$enabled) {
            return new CheckResult(
                $prod ? Status::FAIL : Status::WARN,
                'Mailgun not enabled; production email may not send.',
            );
        }

        $hasUsername = defined('MAILGUN_SMTP_USERNAME') && \MAILGUN_SMTP_USERNAME !== '';
        $hasPassword = defined('MAILGUN_SMTP_PASSWORD') && \MAILGUN_SMTP_PASSWORD !== '';

        if ($hasUsername && $hasPassword) {
            return new CheckResult(Status::OK, 'Mailgun enabled and configured.');
        }

        $missing = [];
        if (!$hasUsername) {
            $missing[] = 'MAILGUN_SMTP_USERNAME';
        }
        if (!$hasPassword) {
            $missing[] = 'MAILGUN_SMTP_PASSWORD';
        }

        return new CheckResult(
            $prod ? Status::FAIL : Status::WARN,
            'Mailgun enabled but credentials missing: ' . implode(', ', $missing) . '.',
        );
    }
}
