<?php

declare(strict_types=1);

namespace Tests\Feature\Account;

use Mtc\MercuryDataModels\Customer;

class ProfileTest extends AccountTestCase
{
    public function testMeRequiresAuth(): void
    {
        $response = $this->asTenant(tenant())->getJson(route('account.me'));
        $response->assertStatus(401);
    }

    public function testMeReturnsAuthenticatedCustomer(): void
    {
        $customer = Customer::create([
            'first_name' => 'Ada',
            'last_name' => 'Lovelace',
            'email' => 'ada@example.com',
            'password' => 'old-secret',
        ]);

        $response = $this->asTenant(tenant())
            ->actingAs($customer, 'customer')
            ->getJson(route('account.me'));

        $response->assertStatus(200);
        $response->assertJsonPath('customer.email', 'ada@example.com');
        $response->assertJsonMissingPath('customer.password');
    }

    public function testUpdateProfile(): void
    {
        $customer = Customer::create([
            'first_name' => 'Ada',
            'last_name' => 'Lovelace',
            'email' => 'ada@example.com',
            'password' => 'old-secret',
            'phone_number' => '555-0100',
        ]);

        $response = $this->asTenant(tenant())
            ->actingAs($customer, 'customer')
            ->patchJson(route('account.me.update'), [
                'first_name' => 'Augusta',
                'last_name' => 'Byron',
                'phone_number' => '555-0199',
                'marketing_email' => true,
                'marketing_phone' => false,
                'marketing_sms' => true,
                'marketing_other' => false,
            ]);

        $response->assertStatus(200);
        $fresh = $customer->fresh();
        $this->assertSame('Augusta', $fresh->first_name);
        $this->assertSame('Byron', $fresh->last_name);
        $this->assertSame('555-0199', $fresh->phone_number);
        $this->assertTrue((bool) $fresh->marketing_email);
        $this->assertFalse((bool) $fresh->marketing_phone);
    }

    public function testUpdatePasswordRequiresCurrent(): void
    {
        $customer = Customer::create([
            'first_name' => 'Ada',
            'last_name' => 'Lovelace',
            'email' => 'ada@example.com',
            'password' => 'old-secret',
        ]);

        $response = $this->asTenant(tenant())
            ->actingAs($customer, 'customer')
            ->patchJson(route('account.password.update'), [
                'current_password' => 'wrong',
                'password' => 'brand-new-password-1',
                'password_confirmation' => 'brand-new-password-1',
            ]);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors(['current_password']);
    }

    public function testUpdatePassword(): void
    {
        $customer = Customer::create([
            'first_name' => 'Ada',
            'last_name' => 'Lovelace',
            'email' => 'ada@example.com',
            'password' => 'old-secret',
        ]);

        $response = $this->asTenant(tenant())
            ->actingAs($customer, 'customer')
            ->patchJson(route('account.password.update'), [
                'current_password' => 'old-secret',
                'password' => 'brand-new-password-1',
                'password_confirmation' => 'brand-new-password-1',
            ]);

        $response->assertStatus(204);
        $this->assertTrue(password_verify('brand-new-password-1', $customer->fresh()->password));
    }
}
