<?php

declare(strict_types=1);

namespace App\Modules\Administration\Services;

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

final class PayamService
{
    public function __construct(
        private PayamRepository $payams
    ) {
    }

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

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

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

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

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

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

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

        if ($countyId < 1) {
            throw new RuntimeException(
                'County is required.'
            );
        }

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

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

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

        if (
            $this->payams->findByName(
                $countyId,
                $name
            ) !== null
        ) {
            throw new RuntimeException(
                'Payam already exists in this county.'
            );
        }

        $this->payams->create([
            'county_id' => $countyId,
            'payam_code' => $code,
            'payam_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 {
        $payam = $this->payams->find($id);

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

        $countyId = (int) (
            $data['county_id'] ?? 0
        );

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

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

        if (
            $countyId < 1
            || $code === ''
            || $name === ''
        ) {
            throw new RuntimeException(
                'County, Payam code and Payam name are required.'
            );
        }

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

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

        $existingName =
            $this->payams->findByName(
                $countyId,
                $name
            );

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

        $this->payams->update(
            $id,
            [
                'county_id' => $countyId,
                'payam_code' => $code,
                'payam_name' => $name,
                'status' => isset($data['status'])
                    ? 1
                    : 0,
                'updated_at' => date('Y-m-d H:i:s')
            ]
        );
    }

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

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

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

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