<?php

namespace Tests\Unit\SiteReadiness\Checks;

use App\SiteReadiness\Checks\SiteEmailsSetCheck;
use App\SiteReadiness\Checks\SiteIdentityCheck;
use App\SiteReadiness\Status;
use Tests\TestCase;

/**
 * Unit tests for SiteEmailsSetCheck and SiteIdentityCheck.
 *
 * MailConfiguredCheck reads the MAILGUN_ENABLED, MAILGUN_SMTP_USERNAME, and
 * MAILGUN_SMTP_PASSWORD constants, which cannot be redefined once set in a
 * process. Its constant-based branch is covered via the route and manual
 * verification path rather than a unit test that would require define().
 * The class is linted and instantiated safely when constants are absent.
 */
class MailIdentityChecksTest extends TestCase
{
    // ------------------------------------------------------------------
    // SiteIdentityCheck
    // ------------------------------------------------------------------

    public function test_identity_with_placeholders_fails_in_production(): void
    {
        config([
            'app.env'                => 'production',
            'site.address.location'  => '1 Example Street',
            'site.address.address_1' => 'Example',
            'site.address.address_2' => 'Example',
            'site.address.postcode'  => 'AA1 AA1',
            'site.address.reg_no'    => '999999',
            'site.address.gphc_no'   => '999999',
            'site.tnx_prefix'        => 'MTC',
        ]);

        $result = (new SiteIdentityCheck())->run();

        $this->assertSame(Status::FAIL, $result->status);
        $this->assertStringContainsString('Not configured:', $result->detail);
    }

    public function test_identity_with_real_values_is_ok(): void
    {
        config([
            'app.env'                => 'production',
            'site.address.location'  => '12 High Street',
            'site.address.address_1' => 'Edinburgh',
            'site.address.address_2' => 'Midlothian',
            'site.address.postcode'  => 'EH1 1YZ',
            'site.address.reg_no'    => '12345678',
            'site.address.gphc_no'   => '1234567',
            'site.tnx_prefix'        => 'RXS',
        ]);

        $result = (new SiteIdentityCheck())->run();

        $this->assertSame(Status::OK, $result->status);
    }

    public function test_identity_with_placeholders_warns_in_non_production(): void
    {
        config([
            'app.env'                => 'staging',
            'site.address.location'  => '1 Example Street',
            'site.address.address_1' => 'Example',
            'site.address.address_2' => 'Example',
            'site.address.postcode'  => 'AA1 AA1',
            'site.address.reg_no'    => '999999',
            'site.address.gphc_no'   => '999999',
            'site.tnx_prefix'        => 'MTC',
        ]);

        $result = (new SiteIdentityCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
    }

    // ------------------------------------------------------------------
    // SiteEmailsSetCheck
    // ------------------------------------------------------------------

    public function test_emails_placeholder_warns(): void
    {
        config([
            'site.from_email'    => 'sales@mtc.co.uk',
            'site.shop_email'    => 'real@example.com',
            'site.contact_email' => 'real@example.com',
        ]);

        $result = (new SiteEmailsSetCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
        $this->assertStringContainsString('from_email', $result->detail);
    }

    public function test_emails_all_real_ok(): void
    {
        config([
            'site.from_email'    => 'noreply@mysite.co.uk',
            'site.shop_email'    => 'orders@mysite.co.uk',
            'site.contact_email' => 'hello@mysite.co.uk',
        ]);

        $result = (new SiteEmailsSetCheck())->run();

        $this->assertSame(Status::OK, $result->status);
    }

    public function test_empty_email_warns(): void
    {
        config([
            'site.from_email'    => '',
            'site.shop_email'    => 'orders@mysite.co.uk',
            'site.contact_email' => 'hello@mysite.co.uk',
        ]);

        $result = (new SiteEmailsSetCheck())->run();

        $this->assertSame(Status::WARN, $result->status);
        $this->assertStringContainsString('from_email', $result->detail);
    }
}
