<?php

namespace Tests\Feature;

use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AuthenticationUiAndSignupTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_login_and_signup_pages_use_the_govix_auth_experience(): void
    {
        $this->get(route('login'))
            ->assertOk()
            ->assertSee('Welcome back')
            ->assertSee('Work smarter.')
            ->assertSee('Mobile OTP')
            ->assertSee('govix-login-hero-v2.webp')
            ->assertSee(route('signup'));

        $this->get(route('signup'))
            ->assertOk()
            ->assertSee('Create Your Workspace')
            ->assertSee('14-day BrandX ERP trial')
            ->assertSee('Typing center name')
            ->assertSee(route('login'));
    }

    public function test_guest_can_create_a_secure_trial_workspace(): void
    {
        $response = $this->post(route('signup.store'), [
            'owner_name' => 'Mariam Al Mazrouei',
            'company_name' => 'Future Gate Typing Center',
            'email' => 'mariam@futuregate.ae',
            'mobile' => '+971501112233',
            'emirate' => 'Dubai',
            'password' => 'SecureTrial123',
            'password_confirmation' => 'SecureTrial123',
            'terms' => '1',
        ]);

        $response->assertRedirect(route('app.onboarding.welcome'));
        $this->assertAuthenticated();

        $tenant = Tenant::where('slug', 'future-gate-typing-center')->firstOrFail();
        $owner = User::where('email', 'mariam@futuregate.ae')->firstOrFail();

        $this->assertSame('trial', $tenant->status);
        $this->assertSame($tenant->id, $owner->tenant_id);
        $this->assertSame('trial', $tenant->subscription->status);
        $this->assertSame('starter', $tenant->subscription->plan->slug);
        $this->assertNotNull($tenant->subscription->trial_ends_at);
        $this->assertTrue($owner->hasRole('owner'));
        $this->assertDatabaseHas('branches', ['tenant_id' => $tenant->id, 'is_main' => true]);
    }

    public function test_signup_rejects_weak_or_unaccepted_registration(): void
    {
        $response = $this->from(route('signup'))->post(route('signup.store'), [
            'owner_name' => 'Test Owner',
            'company_name' => 'Unverified Typing Center',
            'email' => 'owner@unverified.ae',
            'mobile' => '+971501234000',
            'emirate' => 'Dubai',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertRedirect(route('signup'));
        $response->assertSessionHasErrors(['password', 'terms']);
        $this->assertGuest();
        $this->assertDatabaseMissing('tenants', ['slug' => 'unverified-typing-center']);
    }

    public function test_authenticated_user_cannot_create_another_workspace_from_signup(): void
    {
        $existingUser = User::where('email', 'owner@almadina.ae')->firstOrFail();
        $before = Tenant::count();

        $response = $this->actingAs($existingUser)->post(route('signup.store'), [
            'owner_name' => 'Another Owner',
            'company_name' => 'Another Typing Center',
            'email' => 'another@example.ae',
            'mobile' => '+971501234099',
            'emirate' => 'Dubai',
            'password' => 'SecureTrial123',
            'password_confirmation' => 'SecureTrial123',
            'terms' => '1',
        ]);

        $response->assertRedirect(route('home'));
        $this->assertSame($before, Tenant::count());
    }
}
