<?php

namespace App\Models;

use App\Helpers\DeliveryChallanDocumentSettings;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class DeliveryChallan extends Model
{
    protected $fillable = [
        'company_id', 'customer_id', 'challan_number', 'challan_prefix', 'challan_seq',
        'challan_date', 'shipping_date', 'po_label', 'po_no', 'po_date', 'status',
        'place_of_supply', 'is_igst', 'tax_on',
        'subtotal', 'taxable_amount', 'discount_amount',
        'shipping_charges', 'discount_on_total', 'discount_all_percent',
        'discount_all_type', 'discount_all_value', 'custom_amount', 'custom_amount_label',
        'advance_payment', 'has_shipping', 'has_discount_total', 'has_discount_all',
        'has_custom_amount', 'has_advance_payment', 'has_total_quantity',
        'total_quantity_unit', 'total_quantity', 'show_cess',
        'has_transport_eway', 'transport_eway', 'shipping_mode',
        'total_tax', 'cgst_amount', 'sgst_amount', 'igst_amount', 'grand_total',
        'notes', 'terms', 'private_notes', 'share_token',
    ];

    protected $casts = [
        'challan_date' => 'date',
        'shipping_date' => 'date',
        'po_date' => 'date',
        'grand_total' => 'decimal:2',
        'total_tax' => 'decimal:2',
        'total_quantity' => 'decimal:3',
        'has_shipping' => 'boolean',
        'has_discount_total' => 'boolean',
        'has_discount_all' => 'boolean',
        'has_custom_amount' => 'boolean',
        'has_advance_payment' => 'boolean',
        'has_total_quantity' => 'boolean',
        'show_cess' => 'boolean',
        'has_transport_eway' => 'boolean',
        'is_igst' => 'boolean',
    ];

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

    public function items(): HasMany
    {
        return $this->hasMany(DeliveryChallanItem::class)->orderBy('id');
    }

    public function typeLabel(): string
    {
        return DeliveryChallanDocumentSettings::title($this->company_id);
    }

    public function statusMeta(): array
    {
        return config('delivery_challan.statuses.'.$this->status, config('delivery_challan.statuses.draft'));
    }

    public function scopeForCompany(Builder $query, int $companyId): Builder
    {
        return $query->where('company_id', $companyId);
    }

    public function ensureShareToken(): string
    {
        if ($this->share_token) {
            return $this->share_token;
        }

        do {
            $token = Str::lower(Str::random(40));
        } while (static::where('share_token', $token)->exists());

        $this->forceFill(['share_token' => $token])->save();

        return $token;
    }

    public function publicShareUrl(): string
    {
        return route('public.delivery-challans.show', $this->ensureShareToken());
    }
}
