<?php

namespace lib\Services;

use lib\Entities\ClientEntity;
use lib\Entities\MemberEntity;
use lib\Entities\OnetimeEntity as Entity;
use lib\Entities\ServiceEntity;
use lib\Http\Request;
use lib\Models\OnetimeModel as Model;

class OnetimeService extends CommonService
{
  public function __construct()
  {
    parent::__construct();
  }
  final public function getClassName(): string
  {
    return "Onetime";
  }
  final public function getClassPath(): string
  {
    return $this->getClassName();
  }
  public function getModelClass(): string
  {
    return Model::class;
  }
  public function getEntityClass(): string
  {
    return Entity::class;
  }
  public function getCountByServiceCode(string $service_code): int
  {
    $this->getModel()->where("service_code", $service_code);
    return $this->getCount();
  }

  public function getCountByClientCode(string $client_code): int
  {
    $this->getModel()->where("client_code", $client_code);
    return $this->getCount();
  }

  //Coupon 리스트
  public function getEntitiesForDomainCoupon(int $curPage, int $perPage): array
  {
    $this->getModel()->like(["onetime_case" => "%domain%"]);
    $this->getModel()->orderBy("onetime_request_date", "DESC");
    //Query문 Rest여부 -> 같은조건에 Count 받고, 결과값을 받고 싶을때는 continue()
    $this->getModel()->setContinue(true);
    $total = $this->getCount();
    //limit, offset 설정
    $this->getModel()->limit($perPage);
    $this->getModel()->offset(($curPage - 1) * $perPage);
    $entities = $this->getEntities();
    return [$total, $this->getEntities()];
  }

  //도메인쿠폰 사용용
  public function useCouponForDomain(ServiceEntity $service, ClientEntity $client, MemberEntity $member, string $onetime_case, int $coupon, string $note, string $onetime_request_date): bool
  {
    $formDatas = [
      "client_code"           => $service->getClientCode(),
      "service_code"          => $service->getServiceCode(),
      "onetime_case"          => $onetime_case,
      "onetime_sub"           => "도메인 쿠폰 구매 / {$coupon} 개",
      "onetime_amount"        => 0,
      "onetime_payment"       => 0,
      "onetime_nonpayment"    => 0,
      "onetime_accountStatus" => 'complete',
      "onetime_request_date"  => $onetime_request_date,
      "onetime_payment_date"  => $onetime_request_date,
      "onetime_note"          => trim($note),
      "onetime_handle_date"   => $onetime_request_date,
      "onetime_manager"       => $member->getPK(),
      "client_name"           => $client->getTitle(),
      "server_code"           => $service->getServerCode(),
    ];
    return $this->getModel()->insert($formDatas);
  }
}
