<?php

namespace App\Models;

use CodeIgniter\Model;

class FixedAssetModel extends Model
{
    protected $table         = 'fixed_assets';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'number', 'name', 'category_id', 'purchase_date', 'in_service_date',
        'cost', 'salvage_value', 'useful_life_months', 'accum_depreciation', 'book_value',
        'status', 'location', 'asset_account_id', 'accum_depr_account_id',
        'depr_expense_account_id', 'notes', 'created_by', 'updated_by',
    ];

    protected $validationRules = [
        'number'       => 'required|max_length[40]',
        'name'         => 'required|min_length[2]|max_length[200]',
        'category_id'  => 'required|is_natural_no_zero',
        'purchase_date'=> 'required|valid_date',
        'cost'         => 'required|decimal',
        'status'       => 'permit_empty|in_list[active,disposed,fully_depreciated]',
    ];

    public function scopeForList(?string $search = null, ?string $status = null, ?int $categoryId = null): self
    {
        $this->select('fixed_assets.*, asset_categories.name as category_name, asset_categories.code as category_code')
            ->join('asset_categories', 'asset_categories.id = fixed_assets.category_id', 'left');

        if ($search) {
            $this->groupStart()
                ->like('fixed_assets.number', $search)
                ->orLike('fixed_assets.name', $search)
                ->orLike('fixed_assets.location', $search)
                ->orLike('fixed_assets.notes', $search)
                ->orLike('asset_categories.name', $search)
                ->groupEnd();
        }

        if ($status) {
            $this->where('fixed_assets.status', $status);
        }

        if ($categoryId) {
            $this->where('fixed_assets.category_id', $categoryId);
        }

        return $this;
    }

    public function getActiveDepreciable(): array
    {
        return $this->select('fixed_assets.*, asset_categories.name as category_name,
                asset_categories.useful_life_months as category_useful_life_months,
                asset_categories.asset_account_id as category_asset_account_id,
                asset_categories.accum_depr_account_id as category_accum_depr_account_id,
                asset_categories.depr_expense_account_id as category_depr_expense_account_id')
            ->join('asset_categories', 'asset_categories.id = fixed_assets.category_id', 'left')
            ->where('fixed_assets.status', 'active')
            ->orderBy('fixed_assets.number', 'ASC')
            ->findAll();
    }

    public function nextNumber(): string
    {
        $prefix = 'FA-' . date('Ymd') . '-';
        $row = $this->like('number', $prefix, 'after')
            ->orderBy('id', 'DESC')
            ->first();

        $seq = 1;
        if ($row && preg_match('/-(\d+)$/', $row['number'], $m)) {
            $seq = (int) $m[1] + 1;
        }

        return $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
    }
}
