<?php

namespace App\Models;

use App\Models\User;
use App\Models\Address;
use App\Models\OrderProducts;
use App\Traits\LogOptionsTrait;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 * @package App\Models
 * @property integer $id
 * @property integer $user_id
 * @property integer|null $branch_id
 * @property string|null $full_name
 * @property string|null $phone
 * @property string|null $legal_id
 * @property string|null $address_id
 * @property boolean $with_installation
 * @property string $client_type // physical, legal
 * @property string $payment_type // cash, bank, click, payme
 * @property boolean $with_didox
 * @property string $delivery_type // pickup, delivery
 * @property string|null $shipment_date
 * @property string|null $accepted_at
 * @property null $currency
 * @property string $status // processing, collected, waiting_buyer, in_way, closed, cancelled, replacement, completed
 * @property string $payment_status // waiting, cancelled, payed, cash, review
 * @property boolean $archived
 * @property int $price_products
 * @property int $price_delivery
 * @property int $price_total
 * @property int $price_master
 *
 * @property-read User|BelongsTo $user
 * @property-read Address|HasOne $address
 * @property-read Collection|OrdersComment[]|HasMany $comments
 * @property-read UserLegalInfo|BelongsTo $legalInfo
 * @property-read Collection|OrderProducts[]|HasMany $products
 * @property-read Branch|BelongsTo $branch
 * @property-read Status|BelongsTo $getStatus
 * @property-read Status|BelongsTo $getPaymentStatus
 */
class Order extends Model
{
    use LogsActivity, LogOptionsTrait, SoftDeletes;

    protected $guarded = ['id'];

    protected $casts = [
        'user_id' => 'integer',
        'branch_id' => 'integer',
        'with_installation' => 'boolean',
        'with_didox' => 'boolean',
        'shipment_date' => 'datetime',
        'accepted_at' => 'datetime',
        'address_id' => 'integer',
        'archived' => 'boolean',
    ];

    protected static $logName = 'orders';
    protected static $logOnlyDirty = true;

    protected static $logAttributes = [
        'full_name',
        'phone',
        'legal_id',
        'address_id',
        'with_installation',
        'client_type',
        'payment_type',
        'with_didox',
        'delivery_type',
        'shipment_date',
        'accepted_at',
        'currency',
        'status',
        'payment_status',
        'archived',
    ];
    protected static $submitEmptyLogs = false;

    public static function statuses()
    {
        return [
            'processing',
            'collected',
            'waiting_buyer',
            'in_way',
            'closed',
            'cancelled',
            'replacement',
            'completed'
        ];
    }

    public static function paymentStatuses()
    {
        return [
            'waiting',
            'cancelled',
            'payed',
            'cash',
            'review'
        ];
    }

    public static function paymentTypes()
    {
        return [
            'bank',
            'cash',
            'click',
            'payme',
        ];
    }

    public static function clientTypes()
    {
        return [
            'physical',
            'legal',
        ];
    }

    public static function deliveryTypes()
    {
        return [
            'delivery',
            'pickup',
        ];
    }

    public function getStatus()
    {
        return $this->belongsTo(Status::class, 'status', 'slug');
    }

    public function contracts(): HasMany
    {
        return $this->hasMany(OrderContract::class, 'order_id', 'id');
    }

    public function getCurrency(): BelongsTo
    {
        return $this->belongsTo(Currency::class, 'currency', 'id');
    }

    public function getPaymentStatus()
    {
        return $this->belongsTo(Status::class, 'payment_status', 'slug');
    }

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

    public function address(): BelongsTo
    {
        return $this->belongsTo(Address::class, 'address_id', 'id');
    }

    public function comments(): HasMany
    {
        return $this->hasMany(OrdersComment::class, 'order_id', 'id');
    }

    public function legalInfo(): BelongsTo
    {
        return $this->belongsTo(UserLegalInfo::class, 'legal_id', 'id');
    }

    public function products(): HasMany
    {
        return $this->hasMany(OrderProducts::class, 'order_id', 'id');
    }

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

    public function getProductsPrice()
    {
        return $this->products->sum('total_price');
    }

    public function billing()
    {
        return $this->hasOne(Billing::class);
    }
}
