<?php

namespace App\Http\Controllers;

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

class AwardMasterController extends Controller
{
    public function index(Request $request)
    {
        $query = AwardMaster::query();

        
        if ($request->filled('search')) {

            $search = trim($request->search);

            $query->where(function ($q) use ($search) {
                $q->where('Awrd_Name', trim($search))
                    ->orWhere('Awrd_Code', trim($search))
                    ->orWhere('Awrding_Auth', trim($search));
            });
        }

        $data = $query->orderBy('Awrd_Id', 'desc')
            ->paginate($request->per_page ?? 15);

        return response()->json([
            'status' => true,
            'message' => 'Award list fetched successfully',
            'data' => $data
        ]);
    }

    
    public function all()
    {
        $data = AwardMaster::orderBy('Awrd_Name')->get();

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

   public function store(Request $request)
    {
        try {

            $validated = $request->validate([
                'Awrd_Code'     => 'nullable|string|max:20|unique:award_master,Awrd_Code',
                'Awrd_Name'     => 'required|string|max:50',
                'Awrd_Year'     => 'nullable|integer|digits:4',
                'Awrding_Auth'  => 'nullable|string|max:100',
                'Is_Active'     => 'required|boolean',
            ]);

            $award = AwardMaster::create($validated);

            return response()->json([
                'status'  => true,
                'message' => 'Award created successfully',
                'data'    => $award
            ], 201);
        } catch (\Exception $e) {

            return response()->json([
                'status'  => false,
                'message' => 'Failed to create award',
                'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
            ], 500);
        }
    }

    
    public function show($id)
    {
        $award = AwardMaster::find($id);

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

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

     public function update(Request $request, $id)
    {
        try {

            $award = AwardMaster::find($id);

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

            $validated = $request->validate([
                'Awrd_Code'     => 'sometimes|nullable|string|max:20|unique:award_master,Awrd_Code,' . $id . ',Awrd_Id',
                'Awrd_Name'     => 'sometimes|required|string|max:50',
                'Awrd_Year'     => 'sometimes|nullable|integer|digits:4',
                'Awrding_Auth'  => 'sometimes|nullable|string|max:100',
                'Is_Active'     => 'sometimes|required|boolean',
            ]);

            $award->update($validated);

            return response()->json([
                'status'  => true,
                'message' => 'Award updated successfully',
                'data'    => $award
            ]);
        } catch (\Exception $e) {

            return response()->json([
                'status'  => false,
                'message' => 'Failed to update award',
                'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
            ], 500);
        }
    }


    public function destroy($id)
    {
        $award = AwardMaster::find($id);

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

        $award->delete();

        return response()->json([
            'status' => true,
            'message' => 'Award deleted successfully'
        ]);
    }
}
