<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;
use Illuminate\Support\Arr;

class StoreOrder extends BaseModel
{
    use HasFactory;

    protected $table = 'store_order';
    public $timestamps = false;

    public function user(){
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public function client(){
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    
    public function paymentMethod(){
        return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
    }
    public function financialTransactions(){
        return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
    }
    public function finalFinancialTransaction(){
        return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
    }

    public function orderNumber() {
        if ($this->iid) return $this->iid;
        return getFirstSectionUID($this->uid);
    }

    public function tests(){
        $detail = json_decode($this->detail_json);
        $selectedOptions = json_decode(@$detail->selected_options);
        if(!$selectedOptions) return null;
        return (array) @$selectedOptions->tests;
    }

    public function testsRequested(){
        $tests = $this->tests();
        $data = [];
        foreach($tests as $key=>$value){
            if($value){
                array_push($data, $key);
            }
        }
        return $data;
    }

    public function testsRequestedInHumanReadable(){
        $data = [];
        $tests = $this->testsRequested();
        foreach($tests as $test){
            $testName = config('constants.tests.'.$test);
            $testPrice = displayAmount('$', config('app.'.$test));
            $string = $testName . ' - '.$testPrice;
            array_push($data, $string);
        }
        return $data;
    }


    public function total(){
        $detail = json_decode($this->detail_json);
        $selectedOptions = json_decode(@$detail->selected_options);
        if(!$selectedOptions) return 0;
        return floatval(@$selectedOptions->tests_total);
    }

    public function lab(){
        $detail = json_decode($this->detail_json);
        $selectedOptions = json_decode(@$detail->selected_options);
        $labID = @$selectedOptions->tests_lab_id;
        if(!$labID) return null;
        return Lab::where('id', $labID)->first();
    }

    public function createdByUser(){
        return $this->hasOne(User::class, 'id', 'created_by_user_id');
    }

    public function detailJson($toArray = false)
    {
        if($toArray){
            return json_decode($this->detail_json ?? '{}', true);
        }
        return json_decode($this->detail_json ?? '{}');
    }

    public function getDetailJsonValue($field)
    {
        $parsed = $this->detailJson(true);
        if (isset($parsed[$field])) {
            return $parsed[$field];
        }
        return null;
    }
    
}
