<?php

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

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

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

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

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

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

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

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

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

    public function edit(DocumentType $document_type)
    {
        return view('settings::document_types/edit', [
            'documentType' => $document_type,
        ]);
    }

    public function update(Request $request, DocumentType $document_type)
    {
        $request->validate([
            'name' => 'required|string|max:150',
        ], [
            'name.required' => 'Document type name is required.',
            'name.max' => 'Document type name must not exceed 150 characters.',
        ]);

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

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

    public function destroy(DocumentType $document_type)
    {
        // Check if document type is being used by employee documents
        $employeeDocumentCount = DB::table('employee_documents')->where('document_type_id', $document_type->id)->count();

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

        $document_type->delete();

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

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

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