<?php

namespace App\Models;

use App\Http\Helpers\ProfitCalculator;
use App\Http\Helpers\StripeClient;
use App\Notifications\RefundCreated;
use App\Notifications\ServiceOrderDeclined;
use App\Traits\HasMilestones;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ServiceOrder extends Model
{
    use HasFactory, HasMilestones, SoftDeletes;

    protected $guarded = [];

    public function service()
    {
        return $this->belongsTo(Service::class)->withTrashed();
    }

    public function user()
    {
        return $this->belongsTo(User::class)->withTrashed();
    }

    public function workerAccept()
    {
        $this->status = 'in progress';
        $milestone = $this->milestones->first->get();
        $milestone->accepted_at = Carbon::now();
        $milestone->start_date = Carbon::now();
        $milestone->due_date = Carbon::now()->addWeekdays($this->milestones->first->get()->duration);
        $milestone->save();
        $this->save();
        return $this;
    }

    public function accept()
    {
        $this->status = 'accepted';
        $this->save();
        return $this;
    }

    public function decline()
    {

        $transaction = $this->milestones()->first()->transaction; //Transaction::where('milestone_id',->id)->get()->first()
        if($transaction!=null){
            $transaction->refund();
            $this->status = 'declined';
            $this->user->notify(new ServiceOrderDeclined());
            $this->save();
        }

//        $this->milestones()->delete();
        return $this;
    }

    public function refund(?bool $fromAdmin=false){
        $milestoneName = $this->milestones()->first()->name;
        $amount = $this->milestones()->first()->transaction->amount_received;
        $this->status = 'refunded';
        $this->save();
        if($fromAdmin){
            $this->service->serviceable->wallet->activities()
                ->create([
                    'amount' => $amount,
                    'activity_en' => $amount .' has been refunded from your muhandisin wallet to source' ,
                    'activity_ar' => $amount .' تم ردهم الي المصدر من محفظة مهندسين  ',
                    'status' => 'refunded',
                ]);
        }else{
            $this->service->serviceable->wallet->activities()
                ->create(['amount' => $amount,
                    'activity_en' => $amount . ' has been refunded from your muhandisin wallet to source due to decline for ' . $milestoneName,
                    'activity_ar' => $amount . ' تم ردهم الي المصدر من محفظة مهندسين بسبب رفض  ' . $milestoneName,
                    'status' => 'refunded',
                ]);
        }
        $this->service->serviceable->wallet->upcoming_payments -= $amount;
        $this->service->serviceable->wallet->save();
        $this->service->serviceable->notify(new RefundCreated($this->milestones()->first()));
        return $this;
    }
}
