<?php

namespace App\Http\Controllers;

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

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

        $query = DemandRebateTrans::with(['demand', 'demand.property', 'demand.owner', 'demand.taxType',
        'demand.financialYear', 'rebate']);

        if ($search) {
            $query->where('Applied_Amt', 'like', "%$search%")
                  ->orWhere('Dmd_Id', 'like', "%$search%")
                  ->orWhere('Reb_Id', 'like', "%$search%")
      ->orWhereHas('demand', function ($sub) use ($search) {
                $sub->where('Bill_No', 'like', "%$search%");
            })

            ->orWhereHas('rebate', function ($sub) use ($search) {
                $sub->where('Reb_Name', 'like', "%$search%");
            });
        }

        $data = $query->paginate($perPage);

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

    public function all()
    {
        $data = DemandRebateTrans::with(['demand', 'demand.property', 'demand.owner', 'demand.taxType',
        'demand.financialYear', 'rebate'])->get();

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

    public function store(Request $request)
    {
        $request->validate([
            'Dmd_Id' => 'required|exists:trans_demand,Dmd_Id',
            'Reb_Id' => 'required|exists:rebate_master,Reb_Id',
            'Applied_Amt' => 'required|numeric|min:0'
        ]);

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

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

    public function show($id)
    {
        $data = DemandRebateTrans::with(['demand', 'demand.property', 'demand.owner', 'demand.taxType',
        'demand.financialYear', 'rebate'])->find($id);

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

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

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

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

        $request->validate([
            'Dmd_Id' => 'required|exists:trans_demand,Dmd_Id',
            'Reb_Id' => 'required|exists:rebate_master,Reb_Id',
            'Applied_Amt' => 'required|numeric|min:0'
        ]);

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

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

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

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

        $data->delete();

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