<?php

namespace App\Http\Controllers;

use App\Models\Designation;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class DesignationController extends Controller
{
    /**
     * Display the designations listing page.
     */
    public function index()
    {
        return view('designation.index');
    }

    /**
     * Get designations data for DataTable.
     */
    public function datatable(Request $request)
    {
        $query = Designation::with('department:id,name');

        return DataTables::of($query)
            ->addColumn('department_name', function ($designation) {
                return $designation->department?->name;
            })
            ->addColumn('status', function ($designation) {
                return strtolower($designation->status);
            })
            ->make(true);
    }

    /**
     * Store a new designation.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:50|unique:designations,code',
            'department_id' => 'nullable|exists:departments,id',
            'notes' => 'nullable|string',
        ]);

        $designation = Designation::create([
            'name' => $validated['name'],
            'code' => $validated['code'],
            'department_id' => $validated['department_id'] ?? null,
            'notes' => $validated['notes'] ?? null,
            'status' => 'active',
        ]);

        return response()->json([
            'success' => true,
            'message' => __('Designation created successfully.'),
            'data' => $designation,
        ]);
    }

    /**
     * Get a single designation.
     */
    public function show(Designation $designation)
    {
        return response()->json([
            'success' => true,
            'data' => $designation,
        ]);
    }

    /**
     * Update a designation.
     */
    public function update(Request $request, Designation $designation)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:50|unique:designations,code,'.$designation->id,
            'department_id' => 'nullable|exists:departments,id',
            'notes' => 'nullable|string',
        ]);

        $designation->update([
            'name' => $validated['name'],
            'code' => $validated['code'],
            'department_id' => $validated['department_id'] ?? null,
            'notes' => $validated['notes'] ?? null,
        ]);

        return response()->json([
            'success' => true,
            'message' => __('Designation updated successfully.'),
            'data' => $designation,
        ]);
    }

    /**
     * Delete a designation.
     */
    public function destroy(Designation $designation)
    {
        $designation->delete();

        return response()->json([
            'success' => true,
            'message' => __('Designation deleted successfully.'),
        ]);
    }

    /**
     * Toggle designation status.
     */
    public function toggleStatus(Designation $designation)
    {
        $designation->status = strtolower($designation->status) === 'active' ? 'inactive' : 'active';
        $designation->save();

        return response()->json([
            'success' => true,
            'message' => __('Designation status updated successfully.'),
        ]);
    }

    /**
     * Get active designations for dropdown.
     */
    public function getActiveDesignations()
    {
        $designations = Designation::whereRaw('LOWER(status) = ?', ['active'])
            ->get(['id', 'name', 'code']);

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