<?php

namespace App\Tests\Doctor;

use App\Doctor\Entity\Doctor;
use App\Doctor\Entity\DoctorAddress;
use App\Tests\ApiTestCase;

/**
 * GET /clinic-pro/doctor-address/{id} must enforce ownership — a doctor must not
 * read another doctor's address by id enumeration.
 */
class DoctorAddressOwnershipTest extends ApiTestCase
{
    private function makeAddress(): DoctorAddress
    {
        $doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر مالک');
        $this->em->persist($doctor);
        $address = DoctorAddress::forDoctor($doctor)->setName('مطب');
        $this->em->persist($address);
        $this->em->flush();

        return $address;
    }

    public function testOtherDoctorCannotRead(): void
    {
        $address  = $this->makeAddress();
        $stranger = $this->createUser(['ROLE_DOCTOR']);

        $this->authJson('GET', '/api/v1/clinic-pro/doctor-address/' . $address->getId(), $stranger);

        $this->assertSame(403, $this->responseCode());
    }

    public function testOwnerCanRead(): void
    {
        $address = $this->makeAddress();
        $owner   = $address->getDoctor()->getUser();

        $this->authJson('GET', '/api/v1/clinic-pro/doctor-address/' . $address->getId(), $owner);

        $this->assertSame(200, $this->responseCode());
    }
}
