<?php

namespace Modules\Order\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Complaint\Models\Complaint;
use Modules\Rate\Models\Rate;
use Modules\Shipping\Models\Shipping;
use Modules\Store\Models\Branch;
use Modules\Store\Models\Store;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Models\Address;
use Modules\User\Models\User;
use Modules\User\Models\Visitor;
use Modules\User\Services\AccountService;
use Modules\Way\Models\Way;
use Modules\Coupon\Models\Coupon;

class Order extends Model
{
    //use soft delete
    use SoftDeletes;

    protected $fillable =
    [
        'token',
        'payment_id',
        'payment_log',
        'qr_image',
        'status',
        'payment_status',
        'discount',
        'total_tax',
        'shipping_cost',
        'on_hand_cost',
        'total_products',
        'total',
        'note',
        'user_status',
        'user_id',
        'store_id',
        'coupon_id',
        'address_id',
        'way_id',
        'shipping_id'
    ];

    public function getDateAttribute()
    {
        $date = date('d',strtotime($this->created_at));
        $date .= ' '.__('web.monthes.'.date('m',strtotime($this->created_at)));
        $date .= ' '.date('Y',strtotime($this->created_at));
        return $date;
    }

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

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

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

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

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

    public function products()
    {
        return $this->hasMany(OrderProduct::class);
    }
    public function rates()
    {
        return $this->hasMany(Rate::class);
    }
    public function complaints()
    {
        return $this->hasMany(Complaint::class);
    }

    public function user()
    {
        if ($this->user_status == AccountStatusEnum::VISITOR->value)
        {
            $user = $this->belongsTo(Visitor::class,'user_id')->withTrashed();
        }
        else
        {
            $user = $this->belongsTo(User::class,'user_id')->withTrashed();
        }
        return $user;
    }
    public function store()
    {
        return $this->belongsTo(Store::class,'store_id')->withTrashed();
    }
    public function visitor()
    {
        return $this->belongsTo(Visitor::class,'user_id')->withTrashed();
    }
}
