<?php

namespace Tests\Feature;

use App\Models\Conversation;
use App\Models\Message;
use App\Models\User;
use App\Models\EmployerProfile;
use App\Models\Worker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class EmployerMessageApiTest extends TestCase
{
    use RefreshDatabase;

    protected $worker;
    protected $employer;
    protected $token;

    protected function setUp(): void
    {
        parent::setUp();

        // Create a category
        $category = \App\Models\WorkerCategory::create([
            'name' => 'Housemaid',
        ]);

        // Create a test worker
        $this->worker = Worker::create([
            'name' => 'Jane Smith',
            'email' => 'jane@example.com',
            'phone' => '+971500000001',
            'password' => bcrypt('password'),
            'status' => 'Available',
            'nationality' => 'Ethiopian',
            'age' => 28,
            'salary' => 1500.00,
            'availability' => 'Immediate',
            'experience' => '2 Years',
            'religion' => 'Christian',
            'bio' => 'Experienced housemaid seeking employment.',
            'category_id' => $category->id,
        ]);

        // Create an employer user with token
        $this->token = 'employer-test-token-12345';
        $this->employer = User::create([
            'name' => 'John Employer',
            'email' => 'employer_test@example.com',
            'password' => bcrypt('password'),
            'role' => 'employer',
            'api_token' => $this->token,
        ]);

        // Create employer profile
        EmployerProfile::create([
            'user_id' => $this->employer->id,
            'company_name' => 'John Ltd',
            'phone' => '+971500000002',
            'country' => 'United Arab Emirates',
            'verification_status' => 'approved',
            'language' => 'English',
            'notifications' => true,
        ]);
    }

    /**
     * Test employer can login via API.
     */
    public function test_employer_can_login()
    {
        $response = $this->postJson('/api/employers/login', [
            'email' => 'employer_test@example.com',
            'password' => 'password',
        ]);

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data' => [
                    'employer',
                    'token'
                ]
            ]);

        $this->assertNotNull($response->json('data.token'));
    }

    /**
     * Test employer can register via API.
     */
    public function test_employer_can_register()
    {
        $response = $this->postJson('/api/employers/register', [
            'company_name' => 'New Company Corp',
            'name' => 'Alice Employer',
            'email' => 'alice@example.com',
            'phone' => '+971500000003',
            'password' => 'Password@123',
            'password_confirmation' => 'Password@123',
        ]);

        $response->assertStatus(201)
            ->assertJsonStructure([
                'success',
                'message',
                'email'
            ]);

        $this->assertDatabaseHas('users', [
            'email' => 'alice@example.com',
            'role' => 'employer',
        ]);

        $this->assertDatabaseHas('employer_profiles', [
            'company_name' => 'Alice Employer Household',
        ]);
    }

    /**
     * Test retrieving conversation list for an employer.
     */
    public function test_employer_can_get_conversations()
    {
        // Create conversation
        $conversation = Conversation::create([
            'employer_id' => $this->employer->id,
            'worker_id' => $this->worker->id,
        ]);

        // Create a message from worker
        Message::create([
            'conversation_id' => $conversation->id,
            'sender_type' => 'worker',
            'sender_id' => $this->worker->id,
            'text' => 'Hello from candidate!',
        ]);

        // Request API
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/conversations');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'data' => [
                    'conversations' => [
                        '*' => [
                            'id',
                            'worker_id',
                            'worker_name',
                            'category',
                            'nationality',
                            'salary',
                            'last_message',
                            'unread_count',
                            'sent_at',
                            'updated_at',
                        ]
                    ]
                ]
            ]);

        $this->assertEquals('Hello from candidate!', $response->json('data.conversations.0.last_message'));
        $this->assertEquals(1, $response->json('data.conversations.0.unread_count'));
    }

    /**
     * Test retrieving messages in a conversation and marking them as read.
     */
    public function test_employer_can_get_messages_and_mark_as_read()
    {
        // Create conversation
        $conversation = Conversation::create([
            'employer_id' => $this->employer->id,
            'worker_id' => $this->worker->id,
        ]);

        // Create message
        $message = Message::create([
            'conversation_id' => $conversation->id,
            'sender_type' => 'worker',
            'sender_id' => $this->worker->id,
            'text' => 'Unread message from candidate',
            'read_at' => null,
        ]);

        // Request API
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->getJson('/api/employers/conversations/' . $conversation->id . '/messages');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'data' => [
                    'conversation' => [
                        'id',
                        'worker_name',
                        'category',
                        'nationality',
                        'salary',
                    ],
                    'messages' => [
                        '*' => [
                            'id',
                            'sender_type',
                            'text',
                            'time',
                            'read_at',
                            'created_at',
                        ]
                    ]
                ]
            ]);

        // Verify message marked as read in database
        $this->assertNotNull($message->fresh()->read_at);
    }

    /**
     * Test employer can send reply.
     */
    public function test_employer_can_send_reply()
    {
        // Create conversation
        $conversation = Conversation::create([
            'employer_id' => $this->employer->id,
            'worker_id' => $this->worker->id,
        ]);

        // Reply request
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->postJson('/api/employers/conversations/' . $conversation->id . '/messages', [
            'text' => 'Hello Candidate, when are you free for an interview?'
        ]);

        $response->assertStatus(201)
            ->assertJsonStructure([
                'success',
                'message',
                'data' => [
                    'message' => [
                        'id',
                        'sender_type',
                        'text',
                        'time',
                        'created_at',
                    ]
                ]
            ]);

        // Verify message created in database
        $this->assertDatabaseHas('messages', [
            'conversation_id' => $conversation->id,
            'sender_type' => 'employer',
            'sender_id' => $this->employer->id,
            'text' => 'Hello Candidate, when are you free for an interview?',
        ]);
    }

    /**
     * Test employer can start conversation.
     */
    public function test_employer_can_start_conversation()
    {
        $response = $this->withHeaders([
            'Authorization' => 'Bearer ' . $this->token,
        ])->postJson('/api/employers/conversations/start', [
            'worker_id' => $this->worker->id,
        ]);

        $response->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'message',
                'data' => [
                    'conversation_id'
                ]
            ]);

        $this->assertDatabaseHas('conversations', [
            'employer_id' => $this->employer->id,
            'worker_id' => $this->worker->id,
        ]);
    }

    /**
     * Test request fails without authorization token.
     */
    public function test_unauthenticated_requests_are_blocked()
    {
        $response = $this->getJson('/api/employers/conversations');
        $response->assertStatus(401);
    }
}
