<?php

namespace App\Controllers;

use App\Models\BlockchainTransactionModel;

class Blockchain extends BaseController
{
    private BlockchainTransactionModel $model;

    public function __construct()
    {
        $this->model = new BlockchainTransactionModel();
    }

    public function index()
    {
        if ($redirect = $this->requireOrganizationContext()) {
            return $redirect;
        }

        $query = $this->model->withIntervention()
            ->join('sites', 'sites.id = maintenance_interventions.site_id')
            ->join('organizations', 'organizations.id = maintenance_interventions.organization_id');

        if (! $this->isPlatformAdmin()) {
            $query->where('maintenance_interventions.organization_id', (int) $this->currentOrganizationId());
        }

        return view('blockchain/index', [
            'title' => 'Blockchain',
            'active' => 'blockchain',
            'transactions' => $query->orderBy('blockchain_transactions.created_at', 'DESC')->findAll(),
        ]);
    }
}
