<?php

namespace App\Http\Controllers\Admin\Damage;

use App\Http\Controllers\Controller;
use App\Models\DamageMaster;
use App\Models\Supplier;
use App\Service\Damage\DamageService;
use Illuminate\Http\Request;
use Inertia\Inertia;

class DamageController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return Inertia::render('Damage/Damage');
    }

    public function view()
    {

        $damage = DamageMaster::orderby('id', 'DESC')->first();

        $next_number = invoice_no($damage?->id ?? false,  'DM');
        $reference = $next_number;

        $damage = DamageMaster::orderby('id', 'DESC')->with('details')->get();
        $suppliers = Supplier::get();

        return response()->json(['referance' => $reference, 'damage' => $damage, 'suppliers' => $suppliers]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store()
    {
        DamageService::store(request()->all());
        $damage = DamageMaster::orderby('id', 'DESC')->with('details')->first();
        $next_number = invoice_no($damage?->id ?? false,  'DM');
        $reference = $next_number;

        $damage = DamageMaster::orderby('id', 'DESC')->with('details')->get();

        return response()->json(['success' => 'Damage insert Successfully', 'reference' => $reference, 'damage' => $damage]);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        DamageService::update(request()->all(), $id);
        $damage = DamageMaster::orderby('id', 'DESC')->with('details')->get();

        return response()->json(['success' => 'Damage Update Successfully', 'damage' => $damage]);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {

        DamageService::destroy($id);
        $damage = DamageMaster::orderby('id', 'DESC')->first();
        $next_number = invoice_no($damage?->id ?? false,  'DM');
        $reference = $next_number;
        $damage = DamageMaster::orderby('id', 'DESC')->with('details')->get();

 


        return response()->json(['success' => 'Damage Delete Successfully', 'damage' => $damage, 'reference' => $reference]);
    }
}
