<?php

namespace App\Http\Controllers;

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

class ConstructionTypeController extends Controller
{
    
    public function index(Request $request)
    {
        try {
            $perPage = $request->per_page ?? 10;
            $search  = $request->search;

            $query = ConstructionType::query();

            if (!empty($search)) {
                $query->where(function ($q) use ($search) {
                    $q->where('ConsTy_Code', 'like', "%{$search}%")
                      ->orWhere('ConsTy_Name', 'like', "%{$search}%")
                      ->orWhere('Str_Factor', 'like', "%{$search}%");
                });
            }

            $data = $query->orderBy('ConsTy_Id', 'desc')
                          ->paginate($perPage);

            return response()->json([
                'status'  => true,
                'message' => 'Construction Type List',
                'data'    => $data
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'status'  => false,
                'message' => 'Something went wrong. Please try again later.'
            ]);
        }
    }

   
    public function allData()
    {
        try {
            $data = ConstructionType::orderBy('ConsTy_Id', 'desc')->get();

            return response()->json([
                'status'  => true,
                'message' => 'All Construction Type Data',
                'count'   => $data->count(),
                'data'    => $data
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'status'  => false,
                'message' => 'Something went wrong. Please try again later.'
            ]);
        }
    }

   
    public function store(Request $request)
    {
        $request->validate([
           
            'ConsTy_Code' => [
            'nullable',
            \Illuminate\Validation\Rule::unique('construction_types', 'ConsTy_Code')
                ->whereNull('deleted_at')
        ],
            'ConsTy_Name' => 'required',
            'Str_Factor'  => 'nullable|numeric'
        ]);

        $data = ConstructionType::create([
            'ConsTy_Code' => $request->ConsTy_Code,
            'ConsTy_Name' => $request->ConsTy_Name,
            'Str_Factor'  => $request->Str_Factor ?? 1.00
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Created Successfully',
            'data'    => $data
        ]);
    }

    
    public function show($id)
    {
        $data = ConstructionType::find($id);

        if (!$data) {
            return response()->json([
                'status'  => false,
                'message' => 'Record not found'
            ]);
        }

        return response()->json([
            'status' => true,
            'data'   => $data
        ]);
    }

   
    public function update(Request $request, $id)
    {
        $data = ConstructionType::find($id);

        if (!$data) {
            return response()->json([
                'status'  => false,
                'message' => 'Record not found'
            ]);
        }

        $request->validate([
            
            'ConsTy_Code' => [
                'nullable',
                \Illuminate\Validation\Rule::unique('construction_types', 'ConsTy_Code')
                    ->ignore($id, 'ConsTy_Id')
                    ->whereNull('deleted_at')
            ],
            'ConsTy_Name' => 'required',
            'Str_Factor'  => 'nullable|numeric'
        ]);

        $data->update([
            'ConsTy_Code' => $request->ConsTy_Code,
            'ConsTy_Name' => $request->ConsTy_Name,
            'Str_Factor'  => $request->Str_Factor
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Updated Successfully',
            'data'    => $data
        ]);
    }

   
    public function destroy($id)
    {
        $data = ConstructionType::find($id);

        if (!$data) {
            return response()->json([
                'status'  => false,
                'message' => 'Record not found'
            ]);
        }

        $data->delete();

        return response()->json([
            'status'  => true,
            'message' => 'Deleted Successfully'
        ]);
    }
}
