<?php

namespace App\Models;

use App\Concerns\RecordsCreator;
use App\Concerns\ResolvesPublicStorageUrls;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable;

class TemporaryEmployee extends Model implements Auditable
{
    use AuditableTrait, RecordsCreator, ResolvesPublicStorageUrls;

    /**
     * @var list<string>
     */
    protected $auditExclude = [
        'image_path',
        'health_card_path',
    ];

    /**
     * @var list<string>
     */
    protected $fillable = [
        'designation_id',
        'full_name',
        'father_name',
        'national_card_number',
        'address',
        'contact_number',
        'is_active',
        'image_path',
        'health_card_path',
    ];

    /**
     * @var list<string>
     */
    protected $appends = [
        'image_url',
        'health_card_url',
    ];

    /**
     * @var list<string>
     */
    protected $hidden = [
        'image_path',
        'health_card_path',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
        ];
    }

    protected function imageUrl(): Attribute
    {
        return Attribute::make(
            get: fn (): ?string => $this->resolvePublicStorageUrl($this->image_path),
        );
    }

    protected function healthCardUrl(): Attribute
    {
        return Attribute::make(
            get: fn (): ?string => $this->resolvePublicStorageUrl($this->health_card_path),
        );
    }

    /**
     * @return BelongsTo<Designation, $this>
     */
    public function designation(): BelongsTo
    {
        return $this->belongsTo(Designation::class);
    }

    /**
     * @return HasMany<TemporaryEmployeeContract, $this>
     */
    public function contracts(): HasMany
    {
        return $this->hasMany(TemporaryEmployeeContract::class);
    }
}
