<?php

namespace App\Http\Controllers;

use App\Models\CollectionCenter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CollectionCenterController extends Controller
{
    public function index(Request $request)
    {
        $query = CollectionCenter::with('ward');

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

            $query->where(function ($q) use ($search) {
                $q->where('CC_Code', 'like', "%$search%")
                  ->orWhere('CC_Name', 'like', "%$search%")
                  ->orWhere('CC_Type', 'like', "%$search%");
            });
        }

        if ($request->filled('Wrd_Id')) {
            $query->where('Wrd_Id', $request->Wrd_Id);
        }

        if ($request->filled('Is_Active')) {
            $query->where('Is_Active', $request->Is_Active);
        }

        $data = $query->orderBy('CC_Id', 'desc')->paginate(10);

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

public function all(Request $request)
{
    $query = CollectionCenter::with('ward');

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

        $query->where(function ($q) use ($search) {
            $q->where('CC_Code', 'like', "%$search%")
              ->orWhere('CC_Name', 'like', "%$search%")
              ->orWhere('CC_Type', 'like', "%$search%");
        });
    }

    $data = $query->orderBy('CC_Id', 'desc')->get();

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

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
              'CC_Code' => [
        'nullable',
        'max:20',
        \Illuminate\Validation\Rule::unique('collection_centers', 'CC_Code')
            ->whereNull('deleted_at')
    ],

    'Wrd_Id'      => 'nullable|exists:ward_master,Wrd_Id',
    'CC_Name'      => 'required|string|max:150',
    'CC_Type'      => 'nullable|in:Ward Office,Sub-Office,Bank Counter,Online,Mobile Van,CSC',
    'Cash_Limit'   => 'nullable|numeric|max:50000.00',
    'Address'      => 'nullable|string|max:255',
    'Contact_No'   => 'nullable|string|max:15',
    'Is_Active'    => 'nullable|boolean',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => false,
                'errors' => $validator->errors()
            ]);
        }

        $data = CollectionCenter::create($request->all());

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

    public function show($id)
    {
        $data = CollectionCenter::with('ward')->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 = CollectionCenter::find($id);

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

    $validator = Validator::make($request->all(), [

        'CC_Code' => [
            'nullable',
            'max:20',
            \Illuminate\Validation\Rule::unique('collection_centers', 'CC_Code')
                ->ignore($id, 'CC_Id') 
                ->whereNull('deleted_at')
        ],

        'Wrd_Id'      => 'nullable|exists:ward_master,Wrd_Id',
        'CC_Name'     => 'nullable|string|max:150',
        'CC_Type'     => 'nullable|in:Ward Office,Sub-Office,Bank Counter,Online,Mobile Van,CSC',
        'Cash_Limit'  => 'nullable|numeric|max:50000.00',
        'Address'     => 'nullable|string|max:255',
        'Contact_No'  => 'nullable|string|max:15',
        'Is_Active'   => 'nullable|boolean',

    ]);

    if ($validator->fails()) {
        return response()->json([
            'status' => false,
            'errors' => $validator->errors()
        ], 422);
    }

    $data->update($request->all());

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

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

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

        $data->delete();

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