<?php

namespace App\Tests\ClinicService;

use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\Doctor\Entity\Doctor;
use App\Staff\Entity\ClinicStaff;
use App\Tests\ApiTestCase;

/**
 * Binding staff to a service item must be scoped: a tenant must not attach
 * another tenant's staff member via staff_uuid.
 */
class ServiceItemStaffOwnershipTest extends ApiTestCase
{
    public function testCannotBindForeignStaff(): void
    {
        // owner A: their section + item
        $ownerA  = $this->createUser(['ROLE_DOCTOR']);
        $doctorA = new Doctor($ownerA, 'دکتر A');
        $this->em->persist($doctorA);
        $this->em->flush();

        $section = new ServiceSection('doctor', $doctorA->getId(), 'بخش A');
        $item    = new ServiceItem($section, 'خدمت');
        $this->em->persist($section);
        $this->em->persist($item);

        // unrelated tenant B's staff
        $doctorB = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر B');
        $this->em->persist($doctorB);
        $this->em->flush();
        $foreignStaff = new ClinicStaff('doctor', $doctorB->getId(), 'پرسنل B');
        $this->em->persist($foreignStaff);
        $this->em->flush();

        $this->authJson('PATCH', '/api/v1/service-item/' . $item->getUuid(), $ownerA, [
            'staff_uuid' => $foreignStaff->getUuid(),
        ]);

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