<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

use App\Modules\Administration\Repositories\CountyRepository;
use RuntimeException;

final class CountyService
{
    public function __construct(
        private CountyRepository $counties
    ) {
    }

    public function all(): array
    {
        return $this->counties->all();
    }

    public function states(): array
    {
        return $this->counties->states();
    }

    public function find(int $id): ?array
    {
        return $this->counties->find($id);
    }

    public function create(array $data): void
    {
        $stateId = (int) (
            $data['state_id'] ?? 0
        );

        $code = strtoupper(
            trim(
                (string) (
                    $data['county_code'] ?? ''
                )
            )
        );

        $name = trim(
            (string) (
                $data['county_name'] ?? ''
            )
        );

        if ($stateId < 1) {
            throw new RuntimeException(
                'State or Administrative Area is required.'
            );
        }

        if ($code === '') {
            throw new RuntimeException(
                'County code is required.'
            );
        }

        if ($name === '') {
            throw new RuntimeException(
                'County name is required.'
            );
        }

        if (
            $this->counties->findByCode($code)
            !== null
        ) {
            throw new RuntimeException(
                'County code already exists.'
            );
        }

        if (
            $this->counties->findByName(
                $stateId,
                $name
            ) !== null
        ) {
            throw new RuntimeException(
                'County already exists in this location.'
            );
        }

        $this->counties->create([
            'state_id' => $stateId,
            'county_code' => $code,
            'county_name' => $name,
            'status' => 1,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);
    }

    public function update(
        int $id,
        array $data
    ): void {
        $county = $this->counties->find($id);

        if ($county === null) {
            throw new RuntimeException(
                'County not found.'
            );
        }

        $stateId = (int) (
            $data['state_id'] ?? 0
        );

        $code = strtoupper(
            trim(
                (string) (
                    $data['county_code'] ?? ''
                )
            )
        );

        $name = trim(
            (string) (
                $data['county_name'] ?? ''
            )
        );

        if (
            $stateId < 1
            || $code === ''
            || $name === ''
        ) {
            throw new RuntimeException(
                'Location, county code and county name are required.'
            );
        }

        $existingCode =
            $this->counties->findByCode($code);

        if (
            $existingCode !== null
            && (int) $existingCode['id'] !== $id
        ) {
            throw new RuntimeException(
                'County code already exists.'
            );
        }

        $existingName =
            $this->counties->findByName(
                $stateId,
                $name
            );

        if (
            $existingName !== null
            && (int) $existingName['id'] !== $id
        ) {
            throw new RuntimeException(
                'County already exists in this location.'
            );
        }

        $this->counties->update(
            $id,
            [
                'state_id' => $stateId,
                'county_code' => $code,
                'county_name' => $name,
                'status' => isset($data['status'])
                    ? 1
                    : 0,
                'updated_at' => date('Y-m-d H:i:s')
            ]
        );
    }

    public function toggleStatus(int $id): void
    {
        $county = $this->counties->find($id);

        if ($county === null) {
            throw new RuntimeException(
                'County not found.'
            );
        }

        $newStatus =
            (int) $county['status'] === 1
                ? 0
                : 1;

        $this->counties->setStatus(
            $id,
            $newStatus
        );
    }
}