<?php

namespace Modules\Order\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Coupon\Models\Coupon;
use Modules\PaymentWay\Models\PaymentWay;
use Modules\Shipping\Models\Shipping;
use Modules\ShippingWay\Models\ShippingWay;
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\Warehouse\Models\Warehouse;
use Modules\Way\Models\Way;

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',
        'discount_offer',
        'user_status',
        'user_id',
        'coupon_id',
        'address_id',
        'warehouse_id',
        'payment_way_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 getRecieveDateAttribute()
    {
        $date = date('d', strtotime($this->created_at. ' + 1 day'));
        $date .= ' '.__('web.monthes.'.date('m', strtotime($this->created_at. ' + 1 day')));
        $date .= ' '.date('Y', strtotime($this->created_at. ' + 1 day'));
        return $date;
    }

    public function way()
    {
        return $this->belongsTo(PaymentWay::class,'payment_way_id')->withTrashed();
    }

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

    public function shipping()
    {
        return $this->belongsTo(ShippingWay::class,'shipping_way_id')->withTrashed();
    }

    public function products()
    {
        return $this->hasMany(OrderProduct::class)->with(['product','option','store']);
    }
    public function productsStore($store_id)
    {
        return $this->hasMany(OrderProduct::class)->where('store_id',$store_id);
    }

    public function tracks()
    {
        return $this->hasMany(OrderTrack::class);
    }

    public function user()
    {
        $user = $this->belongsTo(User::class,'user_id')->withTrashed();

        return $user;
    }
    public function visitor()
    {
        return $this->belongsTo(Visitor::class,'user_id')->withTrashed();
    }
}
