<?php

namespace App\Http\Livewire\Traits;

use App\Models\ReportDataPartiCoinvolte;
use App\Models\ReportDataPedoni;
use App\Models\ReportDataVeicoli;
use App\Models\Vehicle;

trait HasParteCoinvolta{
    public $anag_type;
    public $parti_coinvolte;

    public function changeAnagType($type) {
        $this->anag_type = $type;
    }

    private function recalculateProgressives() {
        $parti = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->orderBy('progressive')
            ->get();

        foreach ($parti as $index => $parte) {
            if ($parte->progressive != $index) {
                $parte->progressive = $index;
                $parte->save();
                ReportDataVeicoli::where('report_id', $this->dataId)
                    ->where('progressive', $parte->progressive)
                    ->update(['progressive' => $index]);
            }
        }
    }

    public function updateConducentePedoneParteCoinvolta($conducente_o_pedone, $progressive) {
        ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->update(['conducente_o_pedone' => $conducente_o_pedone]);

        if ($conducente_o_pedone === 2) {
            ReportDataVeicoli::updateOrCreate(
                [
                    'report_id' => $this->dataId,
                    'progressive' => $progressive,
                ],
                [
                    'conducente_uguale_proprietario' => false
                ]
            );
        } else {
            ReportDataVeicoli::where('report_id', $this->dataId)
                ->where('progressive', $progressive)
                ->delete();
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function updateAnagraficaParteCoinvolta($anagrafica, $progressive) {
        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->first();

        if ($parte) {
            $parte->update(['anagrafica' => $anagrafica]);

            if ($parte->conducente_o_pedone == 2) {
                ReportDataVeicoli::updateOrCreate(
                    [
                        'report_id' => $this->dataId,
                        'progressive' => $progressive,
                    ],
                    [
                        'conducenti' => $anagrafica
                    ]
                );
            }else{
                ReportDataPedoni::updateOrCreate(
                    [
                        'report_id' => $this->dataId,
                        'progressive' => $progressive,
                    ],
                    [
                        'pedoni' => $anagrafica
                    ]
                );
            }
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function removeAnagraficaParteCoinvolta($progressive){
        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->first();

        if ($parte) {
            $parte->update(['anagrafica' => 0]);

            if ($parte->conducente_o_pedone == 2) {
                ReportDataVeicoli::where('report_id', $this->dataId)
                    ->where('progressive', $progressive)
                    ->update([
                        'conducenti' => null,
                        'conducente_uguale_proprietario' => null
                    ]);
            }
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function updateVeicoloParteCoinvolta($veicolo, $progressive){
        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->first();

        if ($parte && $parte->conducente_o_pedone == 2) {
            $parte->update(['veicolo' => $veicolo]);

            $vehicleInfo = Vehicle::find($veicolo);
            if ($vehicleInfo) {
                ReportDataVeicoli::updateOrCreate(
                    [
                        'report_id' => $this->dataId,
                        'progressive' => $progressive,
                    ],
                    [
                        'veicoli' => $veicolo,
                    ]
                );
            }
        }
        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function removeVeicoloParteCoinvolta($progressive) {
        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->first();

        if ($parte) {
            $parte->update(['veicolo' => 0]);

            if ($parte->conducente_o_pedone == 2) {
                ReportDataVeicoli::where('report_id', $this->dataId)
                    ->where('progressive', $progressive)
                    ->update([
                        'veicoli' => null,
                        'conducenti' => null,
                        'conducente_uguale_proprietario' => null
                    ]);
            }
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function removeParteCoinvolta($progressive){
        $parte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)
            ->where('progressive', $progressive)
            ->first();

        if ($parte) {
            $parte->delete();
            ReportDataVeicoli::where('report_id', $this->dataId)
                ->where('progressive', $progressive)
                ->delete();
            $this->recalculateProgressives();
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }

    public function addParteCoinvolta($type = 'pedone'){
        $count = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->count();
        $progressive = $count == 0 ? 0 : $count;

        ReportDataPartiCoinvolte::create([
            'report_id' => $this->dataId,
            'progressive' => $progressive,
            'progressive_type' => $progressive,
            'conducente_o_pedone' => $type === 'veicolo' ? 2 : 1
        ]);

        if ($type === 'veicolo') {
            ReportDataVeicoli::create([
                'report_id' => $this->dataId,
                'progressive' => $progressive
            ]);
        }else{
            ReportDataPedoni::create([
                'report_id' => $this->dataId,
                'progressive' => $progressive
            ]);
        }

        $this->parti_coinvolte = ReportDataPartiCoinvolte::where('report_id', $this->dataId)->get();
    }
}
