<?php

namespace App\Modules\PLATFORM\Settings\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\PLATFORM\Settings\Models\PaymentMethod;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;

class PaymentMethodController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $query = PaymentMethod::query();

            return DataTables::of($query)
                ->addIndexColumn()
                ->editColumn('status', function ($row) {
                    return $row->status
                        ? '<span class="badge bg-success">Active</span>'
                        : '<span class="badge bg-danger">Inactive</span>';
                })
                ->addColumn('action', function ($row) {
                    return view('settings::payment_methods/partials/actions', compact('row'))->render();
                })
                ->rawColumns(['status', 'action'])
                ->make(true);
        }

        return view('settings::payment_methods/index');
    }

    public function create()
    {
        return view('settings::payment_methods/create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:150',
        ], [
            'name.required' => 'Payment method name is required.',
            'name.max' => 'Payment method name must not exceed 150 characters.',
        ]);

        PaymentMethod::create([
            'name'   => $request->name,
            'status' => $request->boolean('status'),
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Payment method created successfully',
        ]);
    }

    public function edit(PaymentMethod $payment_method)
    {
        return view('settings::payment_methods/edit', [
            'paymentMethod' => $payment_method,
        ]);
    }

    public function update(Request $request, PaymentMethod $payment_method)
    {
        $request->validate([
            'name' => 'required|string|max:150',
        ], [
            'name.required' => 'Payment method name is required.',
            'name.max' => 'Payment method name must not exceed 150 characters.',
        ]);

        $payment_method->update([
            'name'   => $request->name,
            'status' => $request->boolean('status'),
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Payment method updated successfully',
        ]);
    }

    public function destroy(PaymentMethod $payment_method)
    {
        // Check if payment method is being used by employees
        $employeeCount = DB::table('employees')->where('payment_method_id', $payment_method->id)->count();

        if ($employeeCount > 0) {
            return response()->json([
                'status' => false,
                'message' => 'Cannot delete this payment method. It is currently being used by ' . $employeeCount . ' employee(s). Please remove or reassign all employees before deleting this payment method.',
            ], 422);
        }

        $payment_method->delete();

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

    public function status(PaymentMethod $payment_method)
    {
        $payment_method->update([
            'status' => ! $payment_method->status,
        ]);

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