<?php

namespace App\Http\Controllers;

use App\Models\Departmant;
use App\Models\Employee;
use App\Models\EmployeeDepartment;
use App\Models\User;
use Illuminate\Http\Request;

class DepartmantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $departmants = Departmant::all();
        return view('department.index', compact('departmants'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Departmant  $departmant
     * @return \Illuminate\Http\Response
     */
    public function show(Departmant $departmant)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Departmant  $departmant
     * @return \Illuminate\Http\Response
     */
    public function edit(Departmant $departmant)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Departmant  $departmant
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Departmant $departmant)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Departmant  $departmant
     * @return \Illuminate\Http\Response
     */
    public function destroy(Departmant $departmant)
    {
        //
    }

    public function departmentCreate(Request $request)
    {
        $request->validate([
            'name'                => 'required|string|max:255',
            'voucher_software_id' => 'required|integer',
        ]);

        $department = Departmant::updateOrCreate(
            ['voucer_software_id' => $request->voucher_software_id], // search by this
            ['name'                => $request->name]                  // update/create this
        );

        return response()->json([
            'status'  => 'success',
            'message' => $department->wasRecentlyCreated
                ? 'Department created successfully'
                : 'Department updated successfully',
            'data'    => $department,
        ], $department->wasRecentlyCreated ? 201 : 200);
    }


    public function employeeDepartmentCreate(Request $request)
    {
        $request->validate([

            'department_ids' => 'required|array',
            'id' => 'required|integer',
        ]);

        $user = User::where('voucher_softwate_user_id', $request->id)->first();
        if (!$user) {
            return response()->json([
                'status' => 'error',
                'message' => 'User not found',
            ], 404);
        }

        $employee = Employee::where('admin_user_id', $user->id)->first();

        if (!$employee) {
            return response()->json([
                'status' => 'error',
                'message' => 'Employee not found',
            ], 404);
        }

        // delete all employee departments
        EmployeeDepartment::where('employee_id', $employee->id)->delete();

        //all departments
        $departments = Departmant::whereIn('voucer_software_id', $request->department_ids)->get();

        // create new employee departments
        foreach ($departments as $department) {
            EmployeeDepartment::create([
                'employee_id' => $employee->id,
                'department_id' => $department->id,
            ]);
        }

        return response()->json([
            'status' => 'success',
            'message' => 'Employee departments created successfully',
            'data' => $employee->departments,
        ]);
    }
}
