<?php

namespace App\Services\Customer;

use App\Entities\Customer\ServiceEntity;
use App\Entities\Equipment\ServerEntity;
use App\Entities\PaymentEntity;
use App\Helpers\Customer\ServiceHelper;
use App\Models\Customer\ServiceModel;
use App\Services\Equipment\ServerService;
use App\Services\PaymentService;
use DateTimeImmutable;
use DateTimeZone;


class ServiceService extends CustomerService
{
    private ?ServerService $_serverService = null;
    private ?PaymentService $_paymentService = null;
    public function __construct()
    {
        parent::__construct(new ServiceModel(), new ServiceHelper());
        $this->addClassName('Service');
    }
    public function getFormFields(): array
    {
        return [
            "site",
            "location",
            "clientinfo_uid",
            'serverinfo_uid',
            "rack",
            "line",
            "title",
            "start_at",
            "billing_at",
            "status",
            'sale',
            'amount',
            "history",
        ];
    }
    public function getFormFilters(): array
    {
        return [
            'site',
            'location',
            'clientinfo_uid',
            'serverinfo_uid',
            'rack',
            'line',
            'status',
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'site',
            'location',
            'clientinfo_uid',
            'serverinfo_uid',
            'sale',
            'amount',
            'billing_at',
            'status',
            'start_at',
            'updated_at',
        ];
    }
    public function getIndexFilters(): array
    {
        return [
            'site',
            'location',
            'clientinfo_uid',
            'serverinfo_uid',
            'user_uid', //home의 최신신규서버현황에서 사용
            'status',
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['site', 'location', 'clientinfo_uid', 'status'];
    }
    final public function getBatchjobButtons(): array
    {
        return [
            'batchjob' => '일괄 처리 ',
        ];
    }
    public function getFormRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "serverinfo_uid":
                $rule  = "required|numeric";
                break;;
            default:
                $rule = parent::getFormRule($action, $field);
                break;
        }
        return $rule;
    }
    final public function getPaymentService(): PaymentService
    {
        if (!$this->_paymentService) {
            $this->_paymentService = new PaymentService();
        }
        return $this->_paymentService;
    }
    public function getServerService(): ServerService
    {
        if (!$this->_serverService) {
            $this->_serverService = new ServerService();
        }
        return $this->_serverService;
    }
    //interval을 기준으로 최근 신규 서비스정보 가져오기
    final public function getNewServiceEntities(int $interval, string $status = ServiceEntity::DEFAULT_STATUS): array
    {
        return $this->getEntities(sprintf("start_at >= NOW()-INTERVAL {$interval} DAY AND status = '%s'", $status));
    }
    //서비스별 총 금액
    final public function getTotalAmounts($where = []): array
    {
        $rows = $this->getModel()->groupBy('clientinfo_uid')->select("clientinfo_uid,SUM(amount) AS amount")
            ->where($where)
            ->get()->getResult();
        $amounts = [];
        foreach ($rows as $row) {
            $amounts[$row->clientinfo_uid] = $row->amount;
        }
        return $amounts;
    }
    //다음 달로 결제일 가져오기.
    final public function getNextMonthDate(ServiceEntity $entity): string
    {
        // $sql = "UPDATE serviceinfo SET billing_at =
        //         IF(DAY(billing_at) > DAY(LAST_DAY(billing_at)),
        //             LAST_DAY(DATE_ADD(billing_at, INTERVAL 1 MONTH)),
        //             DATE_ADD(billing_at, INTERVAL 1 MONTH)
        //         ) WHERE uid = ?";
        // return $this->getModel()->query($sql, [$entity->getPK()]);
        // 입력된 날짜를 DateTime 객체로 변환
        $date = new DateTimeImmutable($entity->getBillingAt(), new DateTimeZone('Asia/Tokyo'));
        // 현재 일(day)을 저장
        $day = (int)$date->format('d');
        // 다음달로 이동 (DateInterval 사용)
        $date->modify('first day of next month');
        // 다음달의 마지막 날 계산
        $lastDayOfNextMonth = (int)$date->format('t');
        // 현재 날짜가 다음달의 마지막 날보다 크면 -> 마지막 날로 설정
        if ($day > $lastDayOfNextMonth) {
            $day = $lastDayOfNextMonth;
        }
        // 일(day)을 설정
        $date->setDate((int)$date->format('Y'), (int)$date->format('m'), $day);
        // 최종 결과 리턴 (YYYY-MM-DD)
        return $date->format('Y-m-d');
    }
    //총서비스금액 계산
    //기본:상면비+회선비+서버금액(price)+서버파트연결(월비용)-할인액
    public function getCalculatedAmount(ServiceEntity $entity, ServerEntity $serverEntity): int
    {
        return $entity->getRack() + $entity->getLine() + $this->getServerService()->getCalculatedAmount($serverEntity) - $entity->getSale();
    }
    public function setAmount(int $uid): PaymentEntity
    {
        $entity = $this->getEntity($uid);
        if (!$entity instanceof ServiceEntity) {
            throw new \Exception(__METHOD__ . "에서 오류발생: [{$uid}]에 대한 서비스정보를 찾을수 없습니다.");
        }
        $serverEntity = $this->getServerService()->getEntity($entity->getServerInfoUID());
        if (!$serverEntity instanceof ServerEntity) {
            throw new \Exception(__METHOD__ . "에서 오류발생: [{$entity->getServerInfoUID()}]에 대한 서버정보를 찾을수 없습니다.");
        }
        $amount = $this->getCalculatedAmount($entity, $serverEntity);
        $entity =  parent::modify($entity, ['amount' => $amount]);
        //결제정보 수정
        return $this->getPaymentService()->updateForService($entity, $entity->getPaymentUID(), $amount);
    }
    //기본 기능부분
    //FieldForm관련용
    public function getFormOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'serverinfo_uid':
                $options = $this->getServerService()->getEntities();
                break;
            default:
                $options = parent::getFormOption($field, $options);
                break;
        }
        return $options;
    }
    //생성
    public function create(array $formDatas): ServiceEntity
    {
        $db = \Config\Database::connect();
        $db->transStart();

        if (!isset($formDatas['serverinfo_uid'])) {
            throw new \Exception('서버가 지정되지 않았습니다.');
        }
        // 1) 서비스 생성
        $entity = $this->getModel()->create($formDatas);
        // 2) 서버 연결 
        $serverEntity = $this->getServerService()->attachToService($entity, $entity->getServerInfoUID());
        // 3) 금액 계산 + 반영
        $amount = $this->getCalculatedAmount($entity, $serverEntity);
        $this->getModel()->modify($entity, ['amount' => $amount]);
        // 4) 결제 생성 
        $paymentEntity = $this->getPaymentService()->createForService($entity, $amount);
        // 5) 로그
        $this->getMyLogService()->create([
            'title'  => "{$entity->getCustomTitle()} 서비스 추가",
            'status' => $entity->getStatus(),
        ]);
        // 6) 서비스 FK 동기화
        $entity = $this->getModel()->modify($entity, [
            'title'         => $entity->getCustomTitle(),
            'serverinfo_id' => $serverEntity->getPK(),
            'payment_uid'   => $paymentEntity->getPK(),
        ]);

        $db->transComplete();
        if ($db->transStatus() === false) {
            throw new \Exception('트랜잭션 실패');
        }
        return $entity;
    }
    //수정
    public function modify(mixed $entity, array $formDatas): ServiceEntity
    {
        if (!$entity instanceof ServiceEntity) {
            $entity = $this->getEntity((int)$entity);
        }
        $db = \Config\Database::connect();
        $db->transStart();
        if (!isset($formDatas['serverinfo_uid'])) {
            throw new \Exception('변경할 서버가 지정되지 않았습니다.');
        }
        // 서버 변경이면 기존 서버 해지
        if ($entity->getServerInfoUID() !== null && $entity->getServerInfoUID() !== $formDatas['serverinfo_uid']) {
            $this->getServerService()->detachFromService($entity->getServerInfoUID());
        }
        // 1) 서비스 정보 수정
        $entity = $this->getModel()->modify($entity, $formDatas);
        // 2) 서버 재지정
        $serverEntity = $this->getServerService()->attachToService($entity, $entity->getServerInfoUID());
        // 3) 금액 재계산 + 반영
        $amount = $this->getCalculatedAmount($entity, $serverEntity);
        $this->getModel()->modify($entity, ['amount' => $amount]);
        // 4) 결제 수정 (표준 메서드)
        $billingAt = new DateTimeImmutable($entity->getBillingAt(), new DateTimeZone('Asia/Tokyo'));
        $payment = $this->getPaymentService()->updateForService($entity, $entity->getPaymentUID(), $amount);
        // 5) 로그
        $this->getMyLogService()->create([
            'title'  => "{$entity->getCustomTitle()} 서비스 수정",
            'status' => $entity->getStatus(),
        ]);
        // 6) 서비스 FK 동기화
        $entity = $this->getModel()->modify($entity, [
            'title'         => $entity->getCustomTitle(),
            'serverinfo_id' => $serverEntity->getPK(),
            'payment_uid'   => $payment->getPK(),
        ]);
        $db->transComplete();
        if ($db->transStatus() === false) {
            throw new \Exception('트랜잭션 실패');
        }
        return $entity;
    }
    //삭제
    public function delete(mixed $entity): ServiceEntity
    {
        if (!$entity instanceof ServiceEntity) {
            $entity = $this->getEntity((int)$entity);
        }
        $db = \Config\Database::connect();
        $db->transStart();
        // 1) 결제 분리
        if ($entity->getPaymentUID()) {
            $this->getPaymentService()->unlinkFromService($entity->getPaymentUID());
        }
        // 2) 연결된 모든 서버 해지
        foreach ($this->getServerService()->getEntities($entity->getPK()) as $serverEntity) {
            $this->getServerService()->detachFromService($serverEntity->getPK());
        }
        // 3) 로그
        $this->getMyLogService()->create([
            'title'  => "[{$entity->getCustomTitle()}] 서비스 해지",
            'status' => $entity->getStatus(),
        ]);
        // 4) 서비스 삭제
        parent::delete($entity);
        $db->transComplete();
        if ($db->transStatus() === false) {
            throw new \Exception('트랜잭션 실패');
        }
        return $entity;
    }
    //비고(History)설정
    public function history(mixed $entity, array $formDatas): ServiceEntity
    {
        return parent::modify($entity, $formDatas);
    }
    //대체서버추가
    public function addServer(ServiceEntity $entity, array $formDatas): ServiceEntity
    {
        if (!array_key_exists('serverinfo_uid', $formDatas)) {
            throw new \Exception(__METHOD__ . "에서 오류발생:대체서버가 지정되지 않았습니다.");
        }
        $this->getServerService()->attachToService($entity, $formDatas['serverinfo_uid'], [
            'type' => SERVER['TYPES']['ALTERNATIVE'],
        ]);
        return $entity;
    }
    public function changeServer(ServiceEntity $entity, array $formDatas): ServiceEntity
    {
        if (!array_key_exists('serverinfo_uid', $formDatas)) {
            throw new \Exception(__METHOD__ . "에서 오류발생:메인서버로 전환할 대체서버가 지정되지 않았습니다.");
        }
        $serverEntity = $this->getServerService()->getEntity($entity->getServerInfoUID());
        if (!$serverEntity instanceof ServerEntity) {
            throw new \Exception(__METHOD__ . "에서 오류발생: {$entity->getServerInfoUID()}에 대한 서버정보를 찾을수 없습니다.");
        }
        $this->getServerService()->attachToService(
            $entity,
            $formDatas['serverinfo_uid'],
            ['type' => $serverEntity->getType()]
        );
        // 메인 서버 변경에 따른 금액/결제/링크 동기화를 위해 modify 흐름 재사용
        return $this->modify($entity, $formDatas);
    }

    public function terminateServer(ServiceEntity $entity, array $formDatas): ServiceEntity
    {
        if (!array_key_exists('serverinfo_uid', $formDatas)) {
            throw new \Exception(__METHOD__ . "에서 오류발생:메인서버로 전환할 대체서버가 지정되지 않았습니다.");
        }
        if ($entity->getServerInfoUID() === $formDatas['serverinfo_uid']) {
            throw new \Exception(__METHOD__ . "에서 오류발생: 서비스의 메인 서버정보는 해지할 수 없습니다.");
        }
        $this->getServerService()->detachFromService($formDatas['serverinfo_uid']);
        return $entity;
    }
}
