<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ReimbursementBatchAdminFile extends BaseModel
{
    protected $table = 'reimbursement_batch_admin_files';

    /**
     * Your table uses uploaded_at (not created_at/updated_at).
     */
    public $timestamps = false;

    protected $fillable = [
        'batch_id',
        'admin_id',
        'filename',
        'original_filename',
        'uploaded_at',
        'uploaded_by',
    ];

    protected $casts = [
        'batch_id'     => 'integer',
        'admin_id'     => 'integer',
        'uploaded_by'  => 'integer',
        'uploaded_at'  => 'datetime',
    ];

    /* ============================================================
     * Relationships (optional but recommended)
     * ============================================================
     */

    public function batch(): BelongsTo
    {
        return $this->belongsTo(ReimbursementBatch::class, 'batch_id');
    }

    public function admin(): BelongsTo
    {
        // Change Admin to your actual user/admin model if different
        return $this->belongsTo(Admin::class, 'admin_id');
    }

    public function uploader(): BelongsTo
    {
        // If uploaded_by references the same admins/users table
        return $this->belongsTo(Admin::class, 'uploaded_by');
    }

    /* ============================================================
     * Convenience helpers
     * ============================================================
     */

    /**
     * Create an admin file row and auto-set uploaded_at if missing.
     */
    public static function createForBatch(array $data): self
    {
        if (empty($data['uploaded_at'])) {
            $data['uploaded_at'] = now();
        }

        return static::create($data);
    }

    /**
     * Optional: validation rules helper (use in FormRequest/controller).
     */
    public static function rules(bool $updating = false): array
    {
        return [
            'batch_id'           => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:reimbursement_batches,id'],
            'admin_id'           => [$updating ? 'sometimes' : 'required', 'integer', 'min:1', 'exists:admins,id'], // adjust table
            'filename'           => [$updating ? 'sometimes' : 'required', 'string', 'max:255'],
            'original_filename'  => ['nullable', 'string', 'max:255'],
            'uploaded_at'        => ['nullable', 'date'],
            'uploaded_by'        => ['nullable', 'integer'],
        ];
    }
}