<?php

namespace App\Http\Controllers\Member;

use App\Http\Controllers\Controller;
use App\Http\Requests\Member\UpdatePasswordRequest;
use App\Http\Requests\Member\UpdateProfileRequest;
use App\Services\Member\ProfileService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

// ══════════════════════════════════════════════════════════════════
//  InPractice — Member\ProfileController
//  Location: app/Http/Controllers/Member/ProfileController.php
//
//  Thin controller — follows the Project Bible strictly.
//  Every method: receive → call service → return response.
//  Zero business logic here.
// ══════════════════════════════════════════════════════════════════

class ProfileController extends Controller
{
    public function __construct(
        private readonly ProfileService $profileService,
    ) {}

    /**
     * Show the profile & settings page.
     * GET /app/profile
     */
    public function index(): Response
    {
        $data = $this->profileService->show(auth()->user());

        return Inertia::render('Member/Profile/Index', $data);
    }

    /**
     * Update profile identity fields.
     * PATCH /app/profile
     */
    public function update(UpdateProfileRequest $request): JsonResponse
    {
        $this->profileService->updateProfile(
            $request->user(),
            $request->validated()
        );

        return response()->json(['saved' => true]);
    }

    /**
     * Update hub memberships.
     * PATCH /app/profile/hubs
     */
    public function updateHubs(Request $request): JsonResponse
    {
        $request->validate([
            'hub_ids'   => ['required', 'array', 'min:1'],
            'hub_ids.*' => ['integer', 'exists:hubs,id'],
        ]);

        $this->profileService->updateHubs(
            $request->user(),
            $request->hub_ids
        );

        return response()->json(['saved' => true]);
    }

    /**
     * Update privacy settings.
     * PATCH /app/profile/privacy
     */
    public function updatePrivacy(Request $request): JsonResponse
    {
        $request->validate([
            'show_real_name' => ['required', 'boolean'],
        ]);

        $this->profileService->updatePrivacy(
            $request->user(),
            $request->only('show_real_name')
        );

        return response()->json(['saved' => true]);
    }

    /**
     * Update notification preferences.
     * PATCH /app/profile/notifications
     */
    public function updateNotifications(Request $request): JsonResponse
    {
        $request->validate([
            'notify_jobs'      => ['boolean'],
            'notify_freelance' => ['boolean'],
            'notify_forum'     => ['boolean'],
            'notify_surveys'   => ['boolean'],
            'notify_documents' => ['boolean'],
        ]);

        $this->profileService->updateNotifications(
            $request->user(),
            $request->only([
                'notify_jobs',
                'notify_freelance',
                'notify_forum',
                'notify_surveys',
                'notify_documents',
            ])
        );

        return response()->json(['saved' => true]);
    }

    /**
     * Change password.
     * PATCH /app/profile/password
     */
    public function updatePassword(UpdatePasswordRequest $request): JsonResponse
    {
        try {
            $this->profileService->updatePassword(
                $request->user(),
                $request->validated()
            );

            return response()->json(['saved' => true]);

        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 422);
        }
    }
}
