<?php

namespace App\Tests\Secretary;

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

/**
 * Covers the national_code / address fields and the extended permission
 * taxonomy (patients, payments) on the secretary create/update endpoints.
 */
class SecretaryFieldsTest extends ApiTestCase
{
    /** @return array{0: \App\Auth\Entity\User, 1: Doctor} */
    private function makeDoctor(): array
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر تست');
        $this->em->persist($doctor);
        $this->em->flush();

        return [$owner, $doctor];
    }

    public function testCreatePersistsNationalCodeAddressAndExtendedPermissions(): void
    {
        [$owner, $doctor] = $this->makeDoctor();
        $mobile = '09' . str_pad((string) random_int(0, 999_999_999), 9, '0', STR_PAD_LEFT);

        $res = $this->authJson('POST', '/api/v1/secretary', $owner, [
            'doctor_uuid'   => $doctor->getUuid(),
            'mobile_number' => $mobile,
            'name'          => 'سارا احمدی',
            'national_code' => '1234567890',
            'address'       => 'یزد، خیابان تست',
            'permissions'   => [
                'version'   => 1,
                'resources' => [
                    'patients' => ['view' => true, 'create' => true],
                    'payments' => ['view' => true],
                ],
            ],
        ]);

        $this->assertSame(201, $this->responseCode());
        $data = $res['data']['data'] ?? $res['data'];
        $this->assertSame('1234567890', $data['national_code']);
        $this->assertSame('یزد، خیابان تست', $data['address']);
        $this->assertTrue($data['permissions']['patients']['view']);
        $this->assertTrue($data['permissions']['patients']['create']);
        $this->assertTrue($data['permissions']['payments']['view']);
    }

    public function testCreateWithoutOptionalFieldsPersistsNulls(): void
    {
        [$owner, $doctor] = $this->makeDoctor();
        $mobile = '09' . str_pad((string) random_int(0, 999_999_999), 9, '0', STR_PAD_LEFT);

        $res = $this->authJson('POST', '/api/v1/secretary', $owner, [
            'doctor_uuid'   => $doctor->getUuid(),
            'mobile_number' => $mobile,
            'name'          => 'بدون کدملی',
        ]);

        $this->assertSame(201, $this->responseCode());
        $data = $res['data']['data'] ?? $res['data'];
        $this->assertNull($data['national_code']);
        $this->assertNull($data['address']);
    }

    public function testUpdateChangesNameNationalCodeAddressAndPermissions(): void
    {
        [$owner, $doctor] = $this->makeDoctor();
        $mobile = '09' . str_pad((string) random_int(0, 999_999_999), 9, '0', STR_PAD_LEFT);

        $created = $this->authJson('POST', '/api/v1/secretary', $owner, [
            'doctor_uuid'   => $doctor->getUuid(),
            'mobile_number' => $mobile,
            'name'          => 'نام اولیه',
        ]);
        $uuid = ($created['data']['data'] ?? $created['data'])['uuid'];

        $res = $this->authJson('PATCH', '/api/v1/secretary/' . $uuid, $owner, [
            'name'          => 'نام جدید',
            'national_code' => '9999999999',
            'address'       => 'آدرس جدید',
            'permissions'   => [
                'version'   => 1,
                'resources' => ['appointments' => ['create' => false]],
            ],
        ]);

        $this->assertSame(200, $this->responseCode());
        $data = $res['data']['data'] ?? $res['data'];
        $this->assertSame('نام جدید', $data['user_name']);
        $this->assertSame('9999999999', $data['national_code']);
        $this->assertSame('آدرس جدید', $data['address']);
        $this->assertFalse($data['permissions']['appointments']['create']);
    }

    public function testCreateRequiresDoctorUuidAndMobile(): void
    {
        [$owner] = $this->makeDoctor();

        $this->authJson('POST', '/api/v1/secretary', $owner, ['name' => 'ناقص']);

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