<?php

namespace App\Models;

use CodeIgniter\Model;

class BlockchainTransactionModel extends Model
{
    protected $table         = 'blockchain_transactions';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $allowedFields = [
        'maintenance_intervention_id',
        'network',
        'transaction_hash',
        'contract_address',
        'block_number',
        'status',
        'anchor_type',
        'anchored_hash',
    ];

    public function withIntervention()
    {
        return $this->select(
            'blockchain_transactions.*, maintenance_interventions.intervention_code, maintenance_interventions.title'
        )
            ->join(
                'maintenance_interventions',
                'maintenance_interventions.id = blockchain_transactions.maintenance_intervention_id'
            );
    }

    public function forIntervention(int $interventionId): array
    {
        return $this->where('maintenance_intervention_id', $interventionId)
            ->orderBy('created_at', 'DESC')
            ->findAll();
    }
}
