<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Backend\Policy;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Yajra\DataTables\Facades\DataTables;

class PolicyController extends Controller
{
    public function index()
    {
        return view('backend.policy.index');
    }

    public function fetch(Request $request)
    {
        if ($request->ajax()) {
            // Order by serial number ascending
            $data = Policy::orderBy('serial', 'asc')->get();
            $user = auth()->user(); // Get authenticated user

            return DataTables::of($data)
                ->addIndexColumn()
                ->addColumn('description', function ($row) {
                    // Strip HTML tags and limit text for table display
                    return Str::limit(strip_tags($row->description), 60);
                })
                ->addColumn('status', function ($row) {
                    return $row->status == '1'
                        ? '<i class="feather feather-check-circle text-success"></i>'
                        : '<i class="feather feather-x-circle text-danger"></i>';
                })
                ->addColumn('action', function ($row) use ($user) {
                    $btn = '';

                    // Edit Permission Check
                    if ($user->can('edit Policy')) {
                        $btn .= '<button onclick="editPolicy(' . $row->id . ')" class="btn btn-info mr-1" title="Edit"><i class="feather feather-edit"></i></button>';
                    }

                    // Delete Permission Check
                    if ($user->can('delete Policy')) {
                        $btn .= '<button onclick="confirmDelete(' . $row->id . ')" class="btn btn-danger" title="Delete"><i class="feather feather-trash-2"></i></button>';
                    }

                    return $btn ?: '<span class="badge bg-secondary">No Action</span>';
                })
                ->rawColumns(['status', 'action'])
                ->make(true);
        }
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'required',
            'serial' => 'required|integer',
        ]);

        Policy::updateOrCreate(
            ['id' => $request->policy_id],
            [
                'title' => $request->title,
                'description' => $request->description,
                'serial' => $request->serial,
                'status' => $request->status,
            ]
        );

        return response()->json(['success' => 'Policy saved successfully!']);
    }

    public function edit($id)
    {
        $policy = Policy::findOrFail($id);
        return response()->json(['policy' => $policy]);
    }

    public function destroy($id)
    {
        Policy::findOrFail($id)->delete();
        return response()->json(['success' => 'Policy deleted successfully!']);
    }
}
