<?php

namespace App\Http\Controllers;

use App\Models\TaxName;
use Illuminate\Http\Request;

class TaxNameController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $taxNames = TaxName::all();
        return response([
            'success' => true,
            'data' => $taxNames
        ], 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'code' => 'required|string|unique:tax_names,code',
            'name' => 'required|string',
            'description' => 'nullable|string',
        ]);

        $taxName = TaxName::create($request->all());

        return response([
            'success' => true,
            'message' => 'Tax Name created successfully',
            'data' => $taxName
        ], 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $taxName = TaxName::find($id);

        if (!$taxName) {
            return response([
                'success' => false,
                'message' => 'Tax Name not found'
            ], 404);
        }

        return response([
            'success' => true,
            'data' => $taxName
        ], 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $taxName = TaxName::find($id);

        if (!$taxName) {
            return response([
                'success' => false,
                'message' => 'Tax Name not found'
            ], 404);
        }

        $request->validate([
            'code' => 'required|string|unique:tax_names,code,' . $id,
            'name' => 'required|string',
            'description' => 'nullable|string',
        ]);

        $taxName->code = $request->code;
        $taxName->name = $request->name;
        $taxName->description = $request->description;
        $taxName->save();

        return response([
            'success' => true,
            'message' => 'Tax Name updated successfully',
            'data' => $taxName
        ], 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $taxName = TaxName::find($id);

        if (!$taxName) {
            return response([
                'success' => false,
                'message' => 'Tax Name not found'
            ], 404);
        }

        $taxName->delete();

        return response([
            'success' => true,
            'message' => 'Tax Name deleted successfully'
        ], 200);
    }
}
