<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Interfaces\OrganisationRepositoryInterface;
use App\Interfaces\PermissionsRepositoryInterface;
use App\Interfaces\UserRepositoryInterface;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;

class PermissionsController extends Controller
{
    private OrganisationRepositoryInterface $organisationRepository;
    private UserRepositoryInterface $userRepository;
    private PermissionsRepositoryInterface $permissionRepository;


    public function __construct(OrganisationRepositoryInterface $organisationRepository, UserRepositoryInterface $userRepository, PermissionsRepositoryInterface $permissionRepository)
    {
        $this->middleware('auth.api');
        $this->userRepository = $userRepository;
        $this->organisationRepository = $organisationRepository;
        $this->permissionRepository = $permissionRepository;
    }

    //Create Permissions
    public function createPermission(Request $request)
    {
        try {
            //Add Validations
            $validator = Validator::make($request->all(), [
                'name' => 'string|required',
            ]);

            // Check if validation fails
            if ($validator->fails()) {
                return response()->json([
                    'status' => 'failed',
                    'message' => $validator->errors()->first(),
                ], 422);
            }
            //Check if user is superAdmin
            $user = Auth::user();
            $superAdminUser = $this->userRepository->checkIfSuperAdmin($user->id);

            if (!$superAdminUser) {
                return response()->json([
                    'status' => 'failed',
                    'message' => 'You cannot access permissions type',
                ], 401);
            }

            $permissionData = [
                "name" => $request->name,
            ];
            $permissionType = $this->permissionRepository->createPermission($permissionData);

            return response()->json([
                'status' => 'success',
                'message' => 'Permission created successfully',
                'data' => $permissionType
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    //Get All Permissions
    public function getAllPermissions(Request $request)
    {
        try {
            $permissionTypes = $this->permissionRepository->getAllPermissions();
            return response()->json([
                'status' => 'success',
                'message' => 'Permissions retrieved successfully',
                'data' => $permissionTypes
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    public function updatePermission(Request $request)
    { 
        try{
            //Add Validations
            $validator = Validator::make($request->all(), [
                'name' => 'string|required',
            ]);

            // Check if validation fails
            if ($validator->fails()) {
                return response()->json([
                    'status' => 'failed',
                    'message' => $validator->errors()->first(),
                ], 422);
            }

            //Check if user is superAdmin
            $user = Auth::user();

            $superAdminUser = $this->userRepository->checkIfSuperAdmin($user->id);

            if(!$superAdminUser){
                return response()->json([
                    'status' => 'failed',
                    'message' => 'You cannot access permissions type',
                ], 401);
            }

            $permissionId = $request->permissionId;
            $permissionData = [
                "name" => $request->name,
            ];
            $permissionType = $this->permissionRepository->updatePermission($permissionData, $permissionId);

            return response()->json([
                'status' => 'success',
                'message'=> 'Permission Type updated successfully',
                'data' => $permissionType
            ]);
        }catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    //Delete Permission Type
    public function deletePermission(Request $request){
        try{
            $permissionId = $request->permissionId;

            //Check if user is superAdmin
            $user = Auth::user();
            
            $superAdminUser = $this->userRepository->checkIfSuperAdmin($user->id);

            if(!$superAdminUser){
                return response()->json([
                    'status' => 'failed',
                    'message' => 'You cannot access permissions type',
                ], 401);
            }

            $permissionType = $this->permissionRepository->deletePermission($permissionId);

            return response()->json([
                'status' => 'success',
                'message'=> 'Permission type deleted successfully',
                'data' => []
            ]);

        }catch (\Exception $e) {
            return response()->json([
                'status' => 'failed',
                'message' => $e->getMessage(),
            ], 500);
        }
    }
}