<?php
namespace App\Services\Product\Cobon;

use App\Models\BirthCode;
use App\Models\Cobon;
use App\Services\Product\Cobon\CobonWays\CobonBirthService;
use App\Services\Product\Cobon\CobonWays\CobonEventService;
use App\Services\Product\Cobon\CobonWays\CobonNormalService;
use App\Services\Product\Cobon\CobonWays\CobonDiscountService;
use App\Services\Auth\User\AccountService;
use Illuminate\Support\Facades\Auth;


class CobonService
{
    public function calculate($content)
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $status = null;
        
        $cobon = BirthCode::where('date',date('Y-m-d'))->where('code',$content['cobon'])->first();
        
        if($cobon)
        {
            if(Auth::user())
            {
                if(date('m-d',strtotime(Auth::user()->date_of_birth)) != date('m-d'))
                {
                    $cobon = null;
                }
            }
        }
        
        $status = $cobon ? 'birthday' : $status;
        
        if(!$status)
        {
            if(Auth::user())
            {
                if(!$status)
                {
                    $cobon = Cobon::where('cobon_status','event')->where('count','>',0)->where('start_date','<=',date('Y-m-d'))->where('end_date','>=',date('Y-m-d'))->where('code',$content['cobon'])->first();
                    if($cobon)
                    {
                        if($cobon->seller_id == $user->id && $user->role == 'seller')
                        {
                            $cobon = null;
                        }
                    }
                }
            }
            
            $status = $cobon ? 'event' : $status;
        }
        
        if(!$status)
        {
            $cobon = Cobon::where('cobon_status','cobon')->where('count','>',0)->where('start_date','<=',date('Y-m-d'))->where('end_date','>=',date('Y-m-d'))->where('code',$content['cobon'])->first();
            if($cobon)
            {
                if($cobon->seller_id == $user->id && $user->role == 'seller')
                {
                    $cobon = null;
                }
            }
            $status = $cobon ? 'cobon' : $status;
        }
        
        switch ($status) 
        {
            case 'birthday':
                $cobon_way = new CobonDiscountService(new CobonBirthService());
                $response = $cobon_way->calculate($content,$cobon);
                break;
            case 'event':
                $cobon_way = new CobonDiscountService(new CobonEventService());
                $response = $cobon_way->calculate($content,$cobon);
                break;
            case 'cobon':
                $cobon_way = new CobonDiscountService(new CobonNormalService());
                $response = $cobon_way->calculate($content,$cobon);
                break;
            default:
                $response = 
                [
                    'message' => __('web.invalid_cobon'),
                    'discount' => 0,
                    'total' => $content['total'],
                    'status' => null,
                    'cobon' => null,
                    'commission' => 0
                ];
        }

        return $response;
    }

}


