<?php

namespace App\Tests\Patient;

use App\Doctor\Entity\Doctor;
use App\Patient\Entity\PatientRecord;
use App\Tests\ApiTestCase;

/**
 * Patient call log (کال سنتر): create, list, outcome filter, delete + ownership scoping.
 */
class PatientCallTest extends ApiTestCase
{
    /** @return array{0: \App\Auth\Entity\User, 1: PatientRecord} */
    private function recordFor(): array
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر');
        $this->em->persist($doctor);
        $this->em->flush();

        $patient = $this->createUser(['ROLE_USER']);
        $record  = new PatientRecord('doctor', $doctor->getId(), $patient, 'doctor', $doctor->getId());
        $this->em->persist($record);
        $this->em->flush();

        return [$owner, $record];
    }

    public function testCreateListDelete(): void
    {
        [$owner, $record] = $this->recordFor();

        $created = $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, [
            'subject' => 'پیگیری نوبت', 'summary' => 'جابه‌جایی تاریخ', 'outcome' => 'success', 'personnel' => 'مریم امینی',
        ]);
        self::assertSame(201, $this->responseCode());
        self::assertSame('پیگیری نوبت', $created['data']['subject']);
        self::assertSame('success', $created['data']['outcome']);
        self::assertSame('مریم امینی', $created['data']['personnel']);
        $cUuid = $created['data']['uuid'];

        $list = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/calls', $owner);
        self::assertCount(1, $list['data']);

        $this->authJson('DELETE', '/api/v1/patient/call/' . $cUuid, $owner);
        self::assertSame(200, $this->responseCode());

        $after = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/calls', $owner);
        self::assertCount(0, $after['data']);
    }

    public function testRequiresSubject(): void
    {
        [$owner, $record] = $this->recordFor();
        $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, ['subject' => '']);
        self::assertSame(422, $this->responseCode());
    }

    public function testOutcomeFilter(): void
    {
        [$owner, $record] = $this->recordFor();
        $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, ['subject' => 'a', 'outcome' => 'success']);
        $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, ['subject' => 'b', 'outcome' => 'missed']);

        $missed = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/calls?outcome=missed', $owner);
        self::assertCount(1, $missed['data']);
        self::assertSame('missed', $missed['data'][0]['outcome']);
    }

    public function testInvalidOutcomeFallsBackToSuccess(): void
    {
        [$owner, $record] = $this->recordFor();
        $created = $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, [
            'subject' => 'x', 'outcome' => 'garbage',
        ]);
        self::assertSame('success', $created['data']['outcome']);
    }

    public function testOwnershipScoped(): void
    {
        [$owner, $record] = $this->recordFor();
        $created = $this->authJson('POST', '/api/v1/patient/' . $record->getUuid() . '/call', $owner, ['subject' => 'x']);
        $cUuid   = $created['data']['uuid'];

        [$other] = $this->recordFor();
        $this->authJson('DELETE', '/api/v1/patient/call/' . $cUuid, $other);
        self::assertSame(404, $this->responseCode());
        $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/calls', $other);
        self::assertSame(404, $this->responseCode());
    }
}
