<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class BusinessTrip extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'company_id',
        'title',
        'description',
        'destination_city',
        'destination_province',
        'destination_office_id',
        'destination_institution',
        'transportation_type',
        'surat_tugas_number',
        'start_date',
        'end_date',
        'status',
        'approved_by',
        'approved_at',
        'rejection_reason',
        'home_latitude',
        'home_longitude',
    ];

    protected function casts(): array
    {
        return [
            'start_date' => 'date',
            'end_date' => 'date',
            'approved_at' => 'datetime',
            'home_latitude' => 'decimal:8',
            'home_longitude' => 'decimal:8',
        ];
    }

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

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

    public function destinationOffice(): BelongsTo
    {
        return $this->belongsTo(Office::class, 'destination_office_id');
    }

    public function approvedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    public function attendances(): HasMany
    {
        return $this->hasMany(BusinessTripAttendance::class);
    }

    public function documents(): HasMany
    {
        return $this->hasMany(Document::class);
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopeOngoing($query)
    {
        return $query->where('status', 'ongoing');
    }

    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }

    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    public function isFirstDay(): bool
    {
        return now()->toDateString() === $this->start_date->toDateString();
    }

    public function isLastDay(): bool
    {
        return now()->toDateString() === $this->end_date->toDateString();
    }

    public function isOngoing(): bool
    {
        $today = now()->toDateString();
        return $today >= $this->start_date->toDateString()
            && $today <= $this->end_date->toDateString();
    }

    public function getDurationDays(): int
    {
        return $this->start_date->diffInDays($this->end_date) + 1;
    }

    public function getTransportationLabel(): string
    {
        return match ($this->transportation_type) {
            'pesawat' => 'Pesawat',
            'kereta' => 'Kereta Api',
            'bus' => 'Bus',
            'mobil_dinas' => 'Mobil Dinas',
            'mobil_pribadi' => 'Mobil Pribadi',
            'lainnya' => 'Lainnya',
            default => '-',
        };
    }
}
