<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Customer;
use App\Models\CustomerAddress;
use App\Models\Order;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CustomersTest extends TestCase
{
    use RefreshDatabase;

    public function test_customer_resource_page_renders_successfully(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $customer = User::factory()->create([
            'role' => 'customer',
        ]);

        $response = $this->actingAs($admin)->get('/admin/customers');
        $response->assertStatus(200);

        $response = $this->actingAs($admin)->get('/admin/customers/' . $customer->id . '/edit');
        $response->assertStatus(200);
    }

    public function test_only_customers_are_returned_by_customer_model_scope(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
        ]);

        $customer = User::factory()->create([
            'role' => 'customer',
        ]);

        $this->assertCount(1, Customer::all());
        $this->assertEquals($customer->id, Customer::first()->id);
    }

    public function test_creating_customer_sets_role_to_customer(): void
    {
        $customer = Customer::create([
            'name' => 'John Customer',
            'email' => 'john@customer.com',
            'password' => bcrypt('password123'),
        ]);

        $this->assertEquals('customer', $customer->role);
        $this->assertTrue($customer->isCustomer());
    }

    public function test_customer_addresses_relationship(): void
    {
        $customer = User::factory()->create([
            'role' => 'customer',
        ]);

        $address = CustomerAddress::create([
            'customer_id' => $customer->id,
            'street' => '123 Test St',
            'zip' => '12345',
            'city' => 'Test City',
            'country' => 'Test Country',
        ]);

        $this->assertCount(1, $customer->addresses);
        $this->assertEquals('123 Test St', $customer->addresses->first()->street);
    }

    public function test_customer_orders_and_transactions_relationships(): void
    {
        $customer = User::factory()->create([
            'role' => 'customer',
        ]);

        $order = Order::create([
            'user_id' => $customer->id,
            'order_number' => 'ORD-12345',
            'customer_name' => $customer->name,
            'customer_phone' => '123456789',
            'shipping_address' => '123 Test St',
            'subtotal' => 100.00,
            'shipping_cost' => 10.00,
            'grand_total' => 110.00,
            'status' => 'pending',
            'payment_status' => 'pending',
        ]);

        $transaction = Transaction::create([
            'order_id' => $order->id,
            'transaction_id' => 'TXN-98765',
            'payment_method' => 'cod',
            'amount' => 110.00,
            'status' => 'pending',
        ]);

        $this->assertCount(1, $customer->orders);
        $this->assertEquals('ORD-12345', $customer->orders->first()->order_number);

        $this->assertCount(1, $customer->transactions);
        $this->assertEquals('TXN-98765', $customer->transactions->first()->transaction_id);
    }

    public function test_order_does_not_overwrite_customer_details_if_already_present(): void
    {
        $customer = User::factory()->create([
            'role' => 'customer',
            'name' => 'Default Name',
            'phone' => '01700000000',
            'email' => 'default@email.com',
        ]);

        $order = Order::create([
            'user_id' => $customer->id,
            'order_number' => 'ORD-99999',
            'customer_name' => 'Custom Name',
            'customer_phone' => '01899999999',
            'customer_email' => 'custom@email.com',
            'shipping_address' => '123 Test St',
            'subtotal' => 100.00,
            'shipping_cost' => 10.00,
            'grand_total' => 110.00,
            'status' => 'pending',
            'payment_status' => 'pending',
        ]);

        $this->assertEquals('Custom Name', $order->customer_name);
        $this->assertEquals('01899999999', $order->customer_phone);
        $this->assertEquals('custom@email.com', $order->customer_email);

        // Update the order and save to verify it still doesn't overwrite
        $order->status = 'processing';
        $order->save();

        $this->assertEquals('Custom Name', $order->fresh()->customer_name);
        $this->assertEquals('01899999999', $order->fresh()->customer_phone);
        $this->assertEquals('custom@email.com', $order->fresh()->customer_email);
    }
}
