<?php

namespace Modules\User\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\ActivityLogger;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Modules\Auth\Entities\User;
use Modules\User\Entities\Employment;
use Modules\User\Http\Requests\EmploymentRequest;
use Modules\User\Transformers\EmploymentResource;

class UserEmploymentController extends Controller
{
    use AuthorizesRequests;
    public function store(EmploymentRequest $request, string $user): JsonResponse
    {
        $this->authorize('update', User::class);

        try {
            $targetUser = User::query()->find($user);

            if (! $targetUser) {
                return response()->json([
                    'success' => false,
                    'message' => 'User not found.',
                ], 404);
            }

            $employment = $targetUser->employments()->create($request->validated());

            ActivityLogger::log(
                "Created employment for user {$targetUser->name}: {$employment->company_name}",
                $employment
            );

            return response()->json([
                'success' => true,
                'data' => new EmploymentResource($employment),
                'message' => 'Employment created successfully.',
            ], 201);
        } catch (Exception $e) {
            Log::error('Error creating user employment: '.$e->getMessage());

            return response()->json([
                'success' => false,
                'message' => 'Failed to create employment.',
            ], 500);
        }
    }

    public function update(EmploymentRequest $request, string $user, string $employment): JsonResponse
    {
        $this->authorize('update', User::class);

        try {
            $targetUser = User::query()->find($user);

            if (! $targetUser) {
                return response()->json([
                    'success' => false,
                    'message' => 'User not found.',
                ], 404);
            }

            $employmentModel = $targetUser->employments()->find($employment);

            if (! $employmentModel) {
                return response()->json([
                    'success' => false,
                    'message' => 'Employment not found.',
                ], 404);
            }

            $employmentModel->update($request->validated());

            ActivityLogger::log(
                "Updated employment for user {$targetUser->name}: {$employmentModel->company_name}",
                $employmentModel
            );

            return response()->json([
                'success' => true,
                'data' => new EmploymentResource($employmentModel->fresh()),
                'message' => 'Employment updated successfully.',
            ]);
        } catch (Exception $e) {
            Log::error('Error updating user employment: '.$e->getMessage());

            return response()->json([
                'success' => false,
                'message' => 'Failed to update employment.',
            ], 500);
        }
    }

    public function destroy(string $user, string $employment): JsonResponse
    {
        $this->authorize('update', User::class);

        try {
            $targetUser = User::query()->find($user);

            if (! $targetUser) {
                return response()->json([
                    'success' => false,
                    'message' => 'User not found.',
                ], 404);
            }

            $employmentModel = $targetUser->employments()->find($employment);

            if (! $employmentModel) {
                return response()->json([
                    'success' => false,
                    'message' => 'Employment not found.',
                ], 404);
            }

            $companyName = $employmentModel->company_name;
            $employmentModel->delete();

            ActivityLogger::log(
                "Deleted employment for user {$targetUser->name}: {$companyName}",
                $targetUser
            );

            return response()->json([
                'success' => true,
                'message' => 'Employment deleted successfully.',
            ]);
        } catch (Exception $e) {
            Log::error('Error deleting user employment: '.$e->getMessage());

            return response()->json([
                'success' => false,
                'message' => 'Failed to delete employment.',
            ], 500);
        }
    }
}
