<?php

namespace App\Modules\HR\Core\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Core\Models\EmploymentType;
use App\Modules\PLATFORM\Settings\Models\Branch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;

class EmploymentTypeController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $query = EmploymentType::with('branch');

            return DataTables::of($query)
                ->addIndexColumn()
                ->addColumn('branch', fn ($row) => $row->branch?->name ?? '-')
                ->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('core::employment_types/partials/actions', compact('row'))->render();
                })
                ->rawColumns(['status', 'action'])
                ->make(true);
        }

        return view('core::employment_types/index');
    }

    public function create()
    {
        return view('core::employment_types/create', [
            'branches' => Branch::where('status', true)->orderBy('name')->get(),
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name'      => 'required|string|max:150',
            'branch_id' => 'nullable|exists:branches,id',
        ], [
            'name.required'    => 'The employment type name field is required.',
            'name.max'         => 'The employment type name must not exceed 150 characters.',
            'branch_id.exists' => 'The selected branch is invalid.',
        ]);

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

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

    public function edit(EmploymentType $employment_type)
    {
        return view('core::employment_types/edit', [
            'employmentType' => $employment_type,
            'branches'       => Branch::where('status', true)->orderBy('name')->get(),
        ]);
    }

    public function update(Request $request, EmploymentType $employment_type)
    {
        $request->validate([
            'name'      => 'required|string|max:150',
            'branch_id' => 'nullable|exists:branches,id',
        ], [
            'name.required'    => 'The employment type name field is required.',
            'name.max'         => 'The employment type name must not exceed 150 characters.',
            'branch_id.exists' => 'The selected branch is invalid.',
        ]);

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

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

    public function destroy(EmploymentType $employment_type)
    {
        // Check if employment type is used in employees table
        $employeeCount = DB::table('employees')
            ->where('employment_type_id', $employment_type->id)
            ->count();

        if ($employeeCount > 0) {
            return response()->json([
                'status'  => false,
                'message' => 'Cannot delete this employment type because it is being used by ' . $employeeCount . ' employee(s).',
            ], 422);
        }

        $employment_type->delete();

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

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

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