<?php

namespace Modules\Deal\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Deal\Interfaces\DealRepositoryInterface;
use Modules\Deal\Models\Deal;
use Modules\Deal\Models\DealDescription;
use Modules\Deal\Models\DealLog;
use Modules\Deal\Models\DealPayment;
use Modules\Deal\Enums\DealStatusEnum;

class DealRepository implements DealRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Deal::query()
            ->join('deal_descriptions AS sd', 'deals.id', 'sd.deal_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.description', 'deals.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $deal = Deal::find($id);
        $query = DealDescription::where('deal_descriptions.deal_id', $deal->id)
            ->join('deals', 'deal_descriptions.deal_id','deals.id')
            ->join('languages', 'languages.id', 'deal_descriptions.language_id')
            ->select(['deals.*','deal_descriptions.title','deal_descriptions.description', 'languages.local']);

        $dealDescription= $query->get();
        foreach ($dealDescription as $key=>$row)
        {
            $deal[$row->local] = $row;
        }
        return $deal;
    }

    public function checkItem(int $id)
    {
        return Deal::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $deal = Deal::onlyTrashed()->find($id);
        return $deal ;
    }

    public function payments(int $id)
    {
        return DealPayment::where('deal_id',$id)->orderBy('id','ASC')->get();
    }

    public function create(array $request): Deal
    {
        $deal = Deal::create($request);
        return $deal;
    }

    public function update(array $request, ?Deal $deal): Deal
    {
        $deal->update($request);

        return $deal;
    }

    public function saveData(array $request, int $deal_id ) :void
    {
        DealDescription::where('deal_id', $deal_id)->delete();
        foreach (languages() as $lang)
        {
            $data=[
                'deal_id'=> $deal_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'description'=> $request['description_'.$lang->local] ?? null,
            ];
            DealDescription::create($data);
        }
    }

    public function savePayments(array $request, int $deal_id ) :void
    {
        DealPayment::where('deal_id', $deal_id)->delete();
        if(isset($request['payments']))
        {
            foreach ($request['payments'] as $payment)
            {
                $data=[
                    'deal_id'=> $deal_id,
                    'amount'=> $payment,
                ];
                DealPayment::create($data);
            }
        }
    }

    public function saveLog(array $request, int $deal_id ) :void
    {
        $data=
        [
            'deal_id'=> $deal_id,
            'status'=> $request['status'],
        ];
        DealLog::create($data);
    }

    public function confirmedDeal($deal_id)
    {
        return DealLog::where('deal_id',$deal_id)->where('status',DealStatusEnum::Confirmed->value)->orderBy('id','DESC')->first();
    }


    /**
     * Remove the specified Sector from storage.
     * @param Deal $deal
     * @return void
     */
    public function destroy(Deal $deal) :void
    {
        $deal->delete();
    }

    /**
     * Remove several deals by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
        Deal::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified Sector from storage.
     *
     * @param Deal $deal
     * @return void
     */
    public function restore(Deal $deal) :void
    {
        $deal->restore();
    }

    /**
     * Restore several deals by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Deal::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
