<?php
namespace App\Services\Product\Cobon\CobonWays;
use App\Models\Order;
use App\Services\Auth\User\AccountService;

class CobonBirthService implements CobonInterface
{
    public function calculate($content,$cobon)
    {
        $user = (new AccountService())->getAccount();

        $status = $user['status'];
        $user = $user['user'];
        $response = 
        [
            'message' => __('web.invalid_cobon'),
            'discount' => 0,
            'total' => $content['total'],
            'status' => null,
            'cobon' => null,
            'commission' => 0
        ];
        $order = Order::join('order_payments AS op', 'orders.id', 'op.order_id')
            ->select(['op.*', 'orders.*'])->where('cobon_status','birthday')->whereDate('created_at',date('Y-m-d'))->where('user_id',$user->id)->first();
            

        if(!$order)
        {
            $message = __('web.birthday_discount');
            $discount = $content['total'] * (settings('cobon_birth_percentage') / 100 );
            $total = $content['total'] - $discount;
            if($total < 0)
            {
                $discount = $content['total'];
                $total = 0;
            }
            $response =  
            [
                'message' => $message,
                'discount' => $discount,
                'total' => $total,
                'status' => 'birthday',
                'cobon' => null,
                'commission' => 0
            ];
        }
        return $response;
    }
}
