<?php

namespace App\Support;

use App\Models\MoraqebatChecklist;
use App\Models\MoraqebatChecklistBatch;
use App\Models\MoraqebatMachine;

class MoraqebatChecklistCreator
{
    /**
     * @param  array<string, mixed>  $batchAttributes
     * @param  list<array<string, mixed>>  $items
     */
    public static function createBatch(array $batchAttributes, array $items): MoraqebatChecklistBatch
    {
        $batch = MoraqebatChecklistBatch::query()->create([
            'inspection_date' => $batchAttributes['inspection_date'],
            'staff_type' => $batchAttributes['staff_type'],
            'staff_id' => $batchAttributes['staff_id'],
            'is_parts_required' => (bool) ($batchAttributes['is_parts_required'] ?? false),
            'is_machine_downtime' => (bool) ($batchAttributes['is_machine_downtime'] ?? false),
            'is_repair_required' => (bool) ($batchAttributes['is_repair_required'] ?? false),
            'technical_officer_note' => $batchAttributes['technical_officer_note'] ?? null,
        ]);

        foreach ($items as $item) {
            self::createChecklist($batch, $item);
        }

        return $batch;
    }

    /**
     * @param  array<string, mixed>  $attributes
     */
    public static function createChecklist(MoraqebatChecklistBatch $batch, array $attributes): MoraqebatChecklist
    {
        $machineId = (int) $attributes['moraqebat_machine_id'];
        $conditionScore = MoraqebatChecklistCondition::score($attributes);
        $previousChecklist = MoraqebatChecklistCondition::previousForMachine($machineId);

        return MoraqebatChecklist::query()->create([
            'moraqebat_checklist_batch_id' => $batch->id,
            'moraqebat_machine_id' => $machineId,
            'staff_type' => $batch->staff_type,
            'staff_id' => $batch->staff_id,
            'inspection_date' => $batch->inspection_date,
            'machine_state' => $attributes['machine_state'],
            'machine_not_fine_reason' => $attributes['machine_state'] === MoraqebatMachineState::NOT_FINE
                ? ($attributes['machine_not_fine_reason'] ?? null)
                : null,
            'is_oiling_checked' => (bool) ($attributes['is_oiling_checked'] ?? false),
            'is_cleanliness_checked' => (bool) ($attributes['is_cleanliness_checked'] ?? false),
            'is_abnormal_noise' => (bool) ($attributes['is_abnormal_noise'] ?? false),
            'is_observed_issue' => (bool) ($attributes['is_observed_issue'] ?? false),
            'is_action_taken' => (bool) ($attributes['is_action_taken'] ?? false),
            'is_parts_required' => false,
            'is_machine_downtime' => false,
            'is_repair_required' => false,
            'technical_officer_note' => null,
            'condition_score' => $conditionScore,
            'condition_trend' => MoraqebatChecklistCondition::resolveTrend($conditionScore, $previousChecklist),
        ]);
    }

    /**
     * @return list<int>
     */
    public static function activeMachineIds(): array
    {
        return MoraqebatMachine::query()
            ->where('is_active', true)
            ->orderBy('id')
            ->pluck('id')
            ->map(fn ($id): int => (int) $id)
            ->all();
    }
}
