<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\Api\CoachResource;
use App\Http\Resources\Api\MemberResource;
use App\Http\Resources\Api\SecurityResource;
use App\Http\Resources\Api\UserResource;
use App\Http\Resources\Api\VisitorManagementResource;
use App\Models\Coach;
use App\Models\Member;
use App\Models\Security;
use App\Models\VisitorManagement;
use App\Traits\ApiResponseTrait;
use App\Traits\ImageTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class ProfileController extends Controller
{
    use ImageTrait, ApiResponseTrait;


    public function show(Request $request): JsonResponse
    {
        $user = $request->user();

        if (! $user) {
            return $this->errorResponse(__('Unauthenticated.'), 401);
        }

        $resource = $this->resolveResource($user);
        return $this->ApiResponse(new MemberResource($user, 'forShow'), __('Success'), 200);
    }

    public function update(Request $request): JsonResponse
    {
        $user = $request->user();

        if ($user instanceof Member) {
            return $this->updateMemberProfile($request);
        }

        $data = $request->validate([
            'name'  => ['sometimes', 'string', 'max:255'],
            'phone' => ['nullable', 'string', 'max:20'],
            'image' => ['nullable', 'image', 'max:2048'],
        ]);

        $folder = $this->resolveImageFolder($user);
        $this->updateImage($data, 'image', $folder, $user);
        $user->update($data);

        return $this->ApiResponse($this->resolveResource($user->fresh()), __('Profile updated successfully'), 200);
    }

    private function updateMemberProfile(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name'    => ['sometimes', 'string', 'max:255'],
            'email'   => ['sometimes', 'nullable', 'email', 'max:255', 'unique:members,email,' . $request->user()->id],
            'address' => ['sometimes', 'nullable', 'string', 'max:500'],
            'image'   => ['nullable', 'image', 'max:2048'],
        ]);

        $member = $request->user();
        $this->updateImage($validated, 'image', 'members', $member);
        $member->update($validated);

        $member->load(['dependents', 'parkings']);
        return $this->ApiResponse(
            (new MemberResource($member))->forShow(),
            __('Profile updated successfully'),
            200
        );
    }

    public function updatePassword(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'current_password' => ['required', 'current_password'],
            'password'         => ['required', 'confirmed', Password::defaults()],
        ]);

        $request->user()->update([
            'password' => Hash::make($validated['password']),
        ]);

        return $this->ApiResponse([], __('Password updated successfully'), 200);
    }

    private function resolveResource($user)
    {
        if ($user instanceof Member) {
            $user->load(['dependents', 'parkings']);
            return (new MemberResource($user))->forShow();
        }
        if ($user instanceof Coach) {
            return (new CoachResource($user))->forShow();
        }
        if ($user instanceof VisitorManagement) {
            return new VisitorManagementResource($user);
        }
        if ($user instanceof Security) {
            return new SecurityResource($user);
        }
        return new UserResource($user);
    }

    private function resolveImageFolder($user): string
    {
        return match (true) {
            $user instanceof Member            => 'members',
            $user instanceof Coach             => 'coaches',
            $user instanceof VisitorManagement => 'visitor_managements',
            $user instanceof Security         => 'securities',
            default                            => 'users',
        };
    }
}
