<?php

namespace Modules\Internal\Http\Controllers\Api;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\CorporateManager;
use Modules\Internal\Transformers\CorporateManageResource;



class CorporateManageController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request)
    {
        $userCorporateManage = CorporateManager::with(['user', 'corporate'])->get();
      
        return response()->json($userCorporateManage);
    }

    public function store(Request $request)
    {
        $request->validate([
            'user_id' => [
                'required',
            ],
            'corporate_id' => 'required'
        ]);

        $newCorporateManage = CorporateManager::create([
            'corporate_id' => $request->corporate_id,
            'user_id' => $request->user_id,
        ]);

        return $newCorporateManage;
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        $updateCorporateManage = CorporateManager::findOrFail($id);
        $request->validate([
            'user_id' => [
                'required',
            ],
            'corporate_id' => 'required'
        ]);

        $updateCorporateManage->fill([
            'corporate_id' => $request->corporate_id,
            'user_id' => $request->user_id,
        ])->save();

        return $updateCorporateManage;
    }

     /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        // Soft delete model
        $corporateManage = CorporateManager::find($id);
        $corporateManage->delete();

        return $corporateManage;
    }
}
