<?php

namespace App\Services;

use App\Models\Result;

class ResultProcessingService
{
    // Proses untuk AST Result
    public function processAstResult($rawData)
    {
        // Format: R|1| ^ ^ ^AST_MGIT^439400005678^P^0.5^ug/ml| INST_COMPLETE^105^^S| ...
        // Parse raw data menjadi array atau objek
        $resultData = explode('|', $rawData);

        return [
            'test_type'         => 'AST',
            'accession_number'  => $resultData[2],
            'antibiotic'        => $resultData[3], // Seperti 'AST_MGIT'
            'susceptibility'    => $resultData[4], // Seperti 'P' atau 'R'
            'value'             => $resultData[5],
            'status'            => $resultData[6],
            'completion_time'   => $resultData[10]
        ];
    }

    // Proses untuk ID Result
    public function processIdResult($rawData)
    {
        // Format: R|1| ^ ^ ^ID^Seq123|Complete^MYCBTUB^45678^RM_VRE| ...
        $resultData = explode('|', $rawData);

        return [
            'test_type'         => 'ID',
            'sequence_number'   => $resultData[3],
            'result'            => $resultData[4], // Status 'Complete'
            'organism'          => $resultData[5], // Organism name
            'resistance_marker' => $resultData[6], // Resistance marker info
            'completion_time'   => $resultData[8]
        ];
    }

    // Proses untuk Growth and Detection Result
    public function processGrowthDetectionResult($rawData)
    {
        // Format: R|1| ^ ^ ^GND_MGIT^430100001234|INST_POSITIVE ^87| ...
        $resultData = explode('|', $rawData);

        return [
            'test_type'         => 'Growth/Detection',
            'accession_number'  => $resultData[2],
            'status'            => $resultData[3], // Positif atau Negatif
            'completion_time'   => $resultData[7]
        ];
    }

    // Proses untuk Isolate Result
    public function processIsolateResult($rawData)
    {
        // Format: R|1| ^^^AST^^P^100.0^ug/mL| ^^R^R^^MGIT_960_AST92| ...
        $resultData = explode('|', $rawData);

        return [
            'test_type'         => 'Isolate',
            'isolate_result'    => $resultData[3],
            'antibiotic'        => $resultData[4],
            'value'             => $resultData[5],
            'status'            => $resultData[6],
            'completion_time'   => $resultData[10]
        ];
    }

    // Proses untuk Other Test Result
    public function processOtherResult($rawData)
    {
        // Format: R|1| ^ ^ ^OTHER^Seq123|Complete| ...
        $resultData = explode('|', $rawData);

        return [
            'test_type'         => 'Other',
            'sequence_number'   => $resultData[3],
            'result'            => $resultData[4],
            'description'       => $resultData[5],
            'completion_time'   => $resultData[6]
        ];
    }
}

