<?php

namespace App\Tests\Clinic;

use App\Clinic\Entity\Clinic;
use App\Doctor\Entity\DoctorAddress;
use App\Location\Entity\City;
use App\Location\Entity\Province;
use App\Tests\ApiTestCase;

/**
 * GET /api/v1/clinic/{uuid} used to build one response out of two rows: city,
 * province and coordinates came from the clinic's DoctorAddress while the street
 * text and phone still came from the deprecated columns on the clinic itself.
 * A clinic whose address record said Karaj then rendered a Yazd street on the
 * public site. Contact fields must all resolve from the address record, and only
 * fall back to the legacy columns when that record has nothing to offer.
 */
class ClinicContactSourceTest extends ApiTestCase
{
    public function testContactFieldsComeFromTheAddressRecord(): void
    {
        $clinic = $this->createClinic('یزد، خیابان قدیمی', '03531234567');
        $this->createAddressFor($clinic, 'کرج، بلوار طالقانی', '02634567890');

        $data = $this->fetchClinic($clinic);

        $this->assertSame('کرج، بلوار طالقانی', $data['location']);
        $this->assertSame('02634567890', $data['phone']);
        $this->assertSame('02634567890', $data['phone_number']);
        $this->assertSame('کرج', $data['city'][0]['name'] ?? null);
        $this->assertSame('البرز', $data['state'][0]['name'] ?? null);
    }

    public function testLegacyColumnsAreUsedWhenTheClinicHasNoAddressRecord(): void
    {
        $clinic = $this->createClinic('یزد، خیابان قدیمی', '03531234567');

        $data = $this->fetchClinic($clinic);

        $this->assertSame('یزد، خیابان قدیمی', $data['location']);
        $this->assertSame('03531234567', $data['phone']);
        $this->assertSame([], $data['city']);
    }

    public function testBlankAddressFieldsFallBackInsteadOfBlankingTheContactBlock(): void
    {
        $clinic = $this->createClinic('یزد، خیابان قدیمی', '03531234567');
        $this->createAddressFor($clinic, '   ', null);

        $data = $this->fetchClinic($clinic);

        $this->assertSame('یزد، خیابان قدیمی', $data['location']);
        $this->assertSame('03531234567', $data['phone']);
        $this->assertSame('کرج', $data['city'][0]['name'] ?? null);
    }

    private function createClinic(?string $legacyAddress, ?string $legacyPhone): Clinic
    {
        $clinic = new Clinic($this->createUser(['ROLE_CLINIC']));
        $clinic->setName('کلینیک تست تماس');
        $clinic->setAddress($legacyAddress);
        $clinic->setTelephone($legacyPhone);
        $this->em->persist($clinic);
        $this->em->flush();

        return $clinic;
    }

    private function createAddressFor(Clinic $clinic, ?string $street, ?string $phone): DoctorAddress
    {
        $province = new Province('البرز');
        $city     = new City('کرج', $province);
        $this->em->persist($province);
        $this->em->persist($city);

        $address = DoctorAddress::forClinic((int) $clinic->getId());
        $address->setAddress($street);
        $address->setTelephone($phone);
        $address->setCity($city);
        $address->setProvince($province);
        $this->em->persist($address);
        $this->em->flush();

        return $address;
    }

    private function fetchClinic(Clinic $clinic): array
    {
        $this->client->request('GET', '/api/v1/clinic/' . $clinic->getUuid());
        $this->assertSame(200, $this->responseCode());

        return json_decode($this->client->getResponse()->getContent(), true)['data']['data'];
    }
}
