<?php

declare(strict_types=1);

namespace ABS\Modules\SalvageCenter\Controllers;

use ABS\Modules\SalvageCenter\Repositories\SalvageCenterRepository;

final class SalvageCenterController
{
    private SalvageCenterRepository $repo;
    public function __construct(){ $this->repo=new SalvageCenterRepository(); }

    public function index(): void { $vehicles=$this->repo->all(); $this->render('index',compact('vehicles')); }
    public function create(): void { $this->render('form'); }
    public function store(): void
    {
        $num=fn(string $k,float $d=0.0):float => (float)str_replace(',','.',(string)($_POST[$k]??$d));
        $data=[
          'vin'=>strtoupper(trim((string)($_POST['vin']??''))), 'brand'=>trim((string)($_POST['brand']??'')),
          'model'=>trim((string)($_POST['model']??'')), 'production_month'=>(int)($_POST['production_month']??0)?:null,
          'production_year'=>(int)($_POST['production_year']??0)?:null, 'engine'=>trim((string)($_POST['engine']??'')),
          'engine_code'=>strtoupper(trim((string)($_POST['engine_code']??''))), 'power_hp'=>(int)($_POST['power_hp']??0)?:null,
          'emission_standard'=>trim((string)($_POST['emission_standard']??'')), 'gearbox'=>trim((string)($_POST['gearbox']??'')),
          'mileage'=>(int)($_POST['mileage']??0)?:null, 'color'=>trim((string)($_POST['color']??'')),
          'color_code'=>strtoupper(trim((string)($_POST['color_code']??''))), 'damage_area'=>trim((string)($_POST['damage_area']??'')),
          'source'=>trim((string)($_POST['source']??'')), 'purchase_price'=>$num('purchase_price'),
          'transport_cost'=>$num('transport_cost'), 'documents_cost'=>$num('documents_cost'),
          'dismantling_cost'=>$num('dismantling_cost'), 'other_costs'=>$num('other_costs'),
          'desired_profit'=>$num('desired_profit',5000), 'notes'=>trim((string)($_POST['notes']??''))
        ];
        if($data['brand']==='' || $data['model']===''){ $this->redirect('/dezmembrari/create?error=Marca+si+modelul+sunt+obligatorii'); }
        $id=$this->repo->create($data); $this->repo->seedParts($id); $this->redirect('/dezmembrari/'.$id.'?created=1');
    }
    public function show($id): void
    {
        $vehicle=$this->repo->find((int)$id); if(!$vehicle){http_response_code(404); echo 'Vehicul inexistent'; return;}
        $parts=$this->repo->parts((int)$id); $this->render('show',compact('vehicle','parts'));
    }
    public function saveParts($id): void { $this->repo->saveParts((int)$id,$_POST['parts']??[]); $this->redirect('/dezmembrari/'.(int)$id.'?saved=1'); }
    public function updateStatus($id): void { $this->repo->updateStatus((int)$id,(string)($_POST['status']??'evaluation')); $this->redirect('/dezmembrari/'.(int)$id); }
    public function delete($id): void { $this->repo->delete((int)$id); $this->redirect('/dezmembrari?deleted=1'); }
    private function render(string $view,array $vars=[]): void { extract($vars,EXTR_SKIP); require BASE_PATH.'/modules/SalvageCenter/Views/salvage/'.$view.'.php'; }
    private function redirect(string $url): void { header('Location: '.$url); exit; }
}
