<?php

namespace App\Services\Equipment;

use App\DTOs\Equipment\ServerPartDTO;
use App\Entities\CommonEntity;
use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Entities\Part\PartEntity;
use App\Forms\Equipment\ServerPartForm;
use App\Helpers\Equipment\ServerPartHelper;
use App\Models\Equipment\ServerPartModel;
use App\Services\Part\PartService;
use RuntimeException;

class ServerPartService extends EquipmentService
{
    protected string $formClass = ServerPartForm::class;
    protected string $helperClass = ServerPartHelper::class;

    public function __construct(ServerPartModel $model)
    {
        parent::__construct($model);
        $this->addClassPaths('ServerPart');
    }

    public function getDTOClass(): string
    {
        return ServerPartDTO::class;
    }

    public function createDTO(array $formDatas): ServerPartDTO
    {
        return new ServerPartDTO($formDatas);
    }

    public function getEntityClass(): string
    {
        return ServerPartEntity::class;
    }

    private function getPartService(string $type): PartService
    {
        return service('part_' . strtolower($type) . 'service');
    }

    protected function getEntity_process(mixed $entity): ServerPartEntity
    {
        return $entity;
    }

    private function getBillingAtByServiceEntity(ServiceEntity $serviceEntity, string $billing): ?string
    {
        if ($billing === PAYMENT['BILLING']['MONTH']) {
            return $serviceEntity->getBillingAt();
        } elseif ($billing === PAYMENT['BILLING']['ONETIME']) {
            return date("Y-m-d");
        }
        return null;
    }

    private function setPartTitleByPartEntity(array $formDatas): string
    {
        $partEntity = $this->getPartService($formDatas['type'])->getEntity($formDatas['part_uid']);
        if (!$partEntity instanceof PartEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['part_uid']}에 해당하는 파트정보을 찾을수 없습니다.");
        }
        return $partEntity->getTitle();
    }

    private function getFormDatasForServerPart(array $formDatas, ServerEntity $serverEntity): array
    {
        if (empty($formDatas['serverinfo_uid'])) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서버번호가 지정되지 않았습니다.");
        }
        if (empty($formDatas['type'])) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 부품형식이 지정되지 않았습니다.");
        }
        if (empty($formDatas['billing'])) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 청구방법이 지정되지 않았습니다.");
        }
        if (empty($formDatas['part_uid'])) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 파트번호가 지정되지 않았습니다.");
        }

        $formDatas['title'] = $this->setPartTitleByPartEntity($formDatas);

        // ✅ ServerPart는 serverinfo_uid 기준으로 움직인다.
        // serviceinfo_uid는 serverEntity의 "현재 상태"에서만 참고한다.
        $serviceEntity = null;
        if ($serverEntity->getServiceInfoUid()) {
            $serviceEntity = service('customer_serviceservice')->getEntity($serverEntity->getServiceInfoUid());
        }

        $formDatas['billing_at'] = null;
        if ($serviceEntity instanceof ServiceEntity) {
            $formDatas['billing_at'] = $this->getBillingAtByServiceEntity($serviceEntity, $formDatas['billing']);
        }

        $formDatas['clientinfo_uid'] = $serverEntity->getClientInfoUid();
        $formDatas['serviceinfo_uid'] = $serverEntity->getServiceInfoUid();

        return $formDatas;
    }

    /**
     * ✅ MONTH 파트 변경 시:
     * - "서비스가 유지중"이면: (serverinfo_uid 기준) 현재 서버가 붙은 서비스로 amount 재계산 + 월 Payment upsert
     * - "서버가 서비스에서 분리된 상태(server.serviceinfo_uid==null)"이면: 월 미납 결제 status=TERMINATED (amount 재계산 금지)
     *
     * 핵심: ServerPartService는 'serviceUid'가 아니라 'serverUid'를 기준으로 판단한다.
     */
    private function syncMonthlyServiceAndPaymentByServer(
        int $serverUid,
        ?ServiceEntity $oldServiceSnapshot = null,
        bool $serverDetached = false
    ): void {
        $server = service('equipment_serverservice')->getEntity($serverUid);
        if (!$server instanceof ServerEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$serverUid} 서버정보를 찾을수 없습니다.");
        }

        // ✅ 해지 케이스
        if ($serverDetached) {
            if ($oldServiceSnapshot instanceof ServiceEntity) {
                service('paymentservice')->terminateUnpaidMonthlyByService($oldServiceSnapshot);
            }
            return;
        }

        $serviceUid = $server->getServiceInfoUid();
        if (!$serviceUid) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스가 정의되지 않은 서버정보입니다.");
        }

        $svcService = service('customer_serviceservice');
        $current = $svcService->getEntity($serviceUid);
        if (!$current instanceof ServiceEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$serviceUid} 서비스정보를 찾을수 없습니다.");
        }

        // ✅ 핵심 추가: 메인서버만 amount/payment 동기화 허용
        if ((int) $current->getServerInfoUid() !== (int) $serverUid) {
            // 대체서버(alternative)에서는 서비스금액/결제 동기화 금지
            return; // 또는 정책상 필요하면 예외로 변경 가능
            // throw new RuntimeException("메인서버가 아닌 서버({$serverUid})에서 월동기화 금지");
        }

        // ✅ 일반 케이스: amount + payment upsert
        $old = $oldServiceSnapshot instanceof ServiceEntity ? $oldServiceSnapshot : clone $current;
        $svcService->recalcAmountAndSyncPaymentInternal($old, $current);
    }


    protected function create_process(array $formDatas): CommonEntity
    {
        $serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
        if (!$serverEntity instanceof ServerEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
        }

        $formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);

        $entity = parent::create_process($formDatas);
        if (!$entity instanceof ServerPartEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
        }

        // dd($entity);
        $this->getPartService($entity->getType())->attachToServerPart($entity);

        // ✅ 서버가 서비스에 붙어 있을 때만 결제/동기화
        if ($entity->getServiceInfoUid()) {
            if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
                $this->syncMonthlyServiceAndPaymentByServer((int) $entity->getServerInfoUid());
            }
            if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
                service('paymentservice')->createByServerPart($entity);
            }
        }
        return $entity;
    }

    protected function modify_process($entity, array $formDatas): CommonEntity
    {
        $serverEntity = service('equipment_serverservice')->getEntity($formDatas['serverinfo_uid']);
        if (!$serverEntity instanceof ServerEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$formDatas['serverinfo_uid']}에 해당하는 서버정보을 찾을수 없습니다.");
        }

        $formDatas = $this->getFormDatasForServerPart($formDatas, $serverEntity);

        $oldEntity = clone $entity;

        $entity = parent::modify_process($entity, $formDatas);
        if (!$entity instanceof ServerPartEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
        }

        $this->getPartService($entity->getType())->modifyServerPart($oldEntity, $entity);

        // ✅ MONTH 동기화는 serviceinfo_uid가 아니라 serverinfo_uid 기준
        $serverUidsToSync = [];

        if ($oldEntity->getBilling() == PAYMENT['BILLING']['MONTH'] && $oldEntity->getServerInfoUid()) {
            $serverUidsToSync[(int) $oldEntity->getServerInfoUid()] = true;
        }
        if ($entity->getBilling() == PAYMENT['BILLING']['MONTH'] && $entity->getServerInfoUid()) {
            $serverUidsToSync[(int) $entity->getServerInfoUid()] = true;
        }

        foreach (array_keys($serverUidsToSync) as $serverUid) {
            $this->syncMonthlyServiceAndPaymentByServer((int) $serverUid);
        }

        if ($entity->getBilling() == PAYMENT['BILLING']['ONETIME']) {
            service('paymentservice')->modifyByServerPart($oldEntity, $entity);
        }

        return $entity;
    }

    protected function delete_process($entity): CommonEntity
    {
        if (!$entity instanceof ServerPartEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: ServerPartEntity만 가능");
        }

        $old = clone $entity;

        $this->getPartService($entity->getType())->detachFromServerPart($entity);

        $entity = parent::delete_process($entity);
        if (!$entity instanceof ServerPartEntity) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: Return Type은 ServerPartEntity만 가능");
        }

        // ✅ MONTH면 서버 기준으로 서비스/결제 동기화
        if ($old->getBilling() == PAYMENT['BILLING']['MONTH'] && $old->getServerInfoUid()) {
            $this->syncMonthlyServiceAndPaymentByServer((int) $old->getServerInfoUid());
        }

        // ✅ ONETIME 미납 결제 삭제는 "보류" (여기서는 아무것도 안함)
        return $entity;
    }

    //서버추가시 기본파트 자동추가용
    public function attachToServer(ServerEntity $serverEntity): void
    {
        $chassisEntity = service("equipment_chassisservice")->getEntity($serverEntity->getChassisInfoUid());

        foreach (SERVERPART['SERVER_PARTTYPES'] as $parttype) {
            $uid_function = "get{$parttype}InfoUid";
            $cnt_function = "get{$parttype}Cnt";

            $uid = $chassisEntity->$uid_function();
            $cnt = $chassisEntity->$cnt_function();
            if ($uid === null)
                continue;

            $partEntity = $this->getPartService($parttype)->getEntity($uid);
            if (!$partEntity instanceof PartEntity) {
                throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 {$parttype} 파트정보를 찾을수 없습니다.");
            }

            $formDatas = [];
            $formDatas['serverinfo_uid'] = $serverEntity->getPK();
            $formDatas["part_uid"] = $partEntity->getPK();
            $formDatas['billing'] = PAYMENT['BILLING']['BASE'];
            $formDatas['type'] = $parttype;
            $formDatas['title'] = $partEntity->getTitle();
            $formDatas['amount'] = $partEntity->getPrice();
            $formDatas['cnt'] = $cnt;

            $this->create_process($formDatas);
        }
    }

    /**
     * ✅ 서버 해지/분리 시 서버파트 회수 처리
     * - BASE 제외 전부 삭제(정책상 OK)
     * - MONTH 삭제가 있었다면:
     *    - server.serviceinfo_uid == null 인 상태에서만 TERMINATED 처리 (amount 재계산 금지)
     * - ONETIME 미납 삭제는 보류
     *
     * @param ServiceEntity|null $oldServiceEntity  분리 전 서비스 스냅샷(권장: TERMINATED 매칭용)
     */
    public function detachFromServer(ServerEntity $serverEntity, ?ServiceEntity $oldServiceEntity = null): void
    {
        $monthlyChanged = false;
        foreach ($this->getEntities(['serverinfo_uid' => $serverEntity->getPK(), "billing !=" => PAYMENT['BILLING']['BASE']]) as $entity) {
            if (!$entity instanceof ServerPartEntity)
                continue;
            $this->getPartService($entity->getType())->detachFromServerPart($entity);
            if ($entity->getBilling() == PAYMENT['BILLING']['MONTH']) {
                $monthlyChanged = true;
            }
            parent::delete_process($entity);
        }
        if (!$monthlyChanged) {
            return;
        }
        // ✅ 분리 완료 후(server.serviceinfo_uid == null)에서만 TERMINATED 처리
        $serverDetached = ($serverEntity->getServiceInfoUid() === null);
        if ($serverDetached) {
            if ($oldServiceEntity instanceof ServiceEntity) {
                $this->syncMonthlyServiceAndPaymentByServer((int) $serverEntity->getPK(), $oldServiceEntity, true);
            }
            return;
        }
        // ✅ (이 케이스는 정상 플로우에서는 거의 없지만) 서비스 유지중이면 정상 upsert
        $this->syncMonthlyServiceAndPaymentByServer((int) $serverEntity->getPK());
    }
}
