<?php

namespace App\Models;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Export extends Model
{
    use HasFactory;
    // use Searchable;

    protected $guarded = ['id'];
    protected $casts = [
        'price' => 'float',
        'total' => 'float',
        'amount' => 'float',
    ];
    protected $appends = ['total'];
    protected $with = ['category'];

    // public function toSearchableArray()
    // {
    //     return [
    //         'id' => $this->id,
    //         'title' => $this->title,
    //         'category' => $this->category_id,
    //         'group' => $this->group_id,
    //         'locale' => $this->locale,
    //     ];
    // }

    public function scopeLines($query)
    {
        return $query->where('is_line', true);
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public static function countries($ids = [])
    {
        return self::selectRaw('country')
            ->where('group_id', request('group'))
            ->where('locale', app()->getLocale())
            // ->whereIn('id', $ids)
            // ->when(request()->has('unit'), fn ($q) => $q->where('unit', request('unit')))
            // ->when(request()->has('currency'), fn ($q) => $q->where('currency', request('currency')))
            ->distinct()
            ->pluck('country');
    }

    public static function units($ids = [])
    {
        return self::selectRaw('unit')
            ->where('group_id', request('group'))
            ->where('locale', app()->getLocale())
            // ->whereIn('id', $ids)
            ->distinct()
            ->pluck('unit');
    }

    public static function currencies($ids = [])
    {
        return self::selectRaw('currency')
            ->where('group_id', request('group'))
            ->where('locale', app()->getLocale())
            // ->whereIn('id', $ids)
            ->distinct()
            ->pluck('currency');
    }

    public static function payments($ids = [])
    {
        return self::selectRaw('payment')
            ->where('group_id', request('group'))
            ->where('locale', app()->getLocale())
            // ->whereIn('id', $ids)
            ->distinct()
            ->pluck('payment');
    }

    public static function sends($ids = [])
    {
        return self::selectRaw('send')
            ->where('group_id', request('group'))
            ->where('locale', app()->getLocale())
            // ->whereIn('id', $ids)
            ->distinct()
            ->pluck('send');
    }

    public function getTotalAttribute()
    {
        return round($this->price * $this->amount, 2);
    }
}
