<?php

namespace App\Services\Member;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;

// ══════════════════════════════════════════════════════════════════
//  InPractice — ProfileService
//  Location: app/Services/Member/ProfileService.php
//
//  All business logic for the member profile & settings page.
//  Controller stays thin — it only calls these methods.
// ══════════════════════════════════════════════════════════════════

class ProfileService
{
    /**
     * Return all data the profile page needs.
     */
    public function show(User $user): array
    {
        $user->load('hubs:id,name_en,name_ar,slug,color');

        return [
            'user' => [
                'id'               => $user->id,
                'name'             => $user->name,
                'nickname'         => $user->nickname,
                'email'            => $user->email,
                'profession'       => $user->profession,
                'experience_level' => $user->experience_level,
                'sector'           => $user->sector,
                'bio'              => $user->bio,
                'avatar'           => $user->avatar,
                'language'         => $user->language,
                'theme'            => $user->theme ?? 'navy',
                'show_real_name'   => (bool) $user->show_real_name,
                'notify_jobs'      => (bool) $user->notify_jobs,
                'notify_freelance' => (bool) $user->notify_freelance,
                'notify_forum'     => (bool) $user->notify_forum,
                'notify_surveys'   => (bool) $user->notify_surveys,
                'notify_documents' => (bool) $user->notify_documents,
                'joined_at'        => $user->created_at->format('M Y'),
                'hubs'             => $user->hubs->map(fn($h) => [
                    'id'      => $h->id,
                    'name_en' => $h->name_en,
                    'name_ar' => $h->name_ar,
                    'slug'    => $h->slug,
                    'color'   => $h->color,
                ]),
                'highly_rated_solutions_count' => (int) ($user->highly_rated_solutions_count ?? 0),
                'highly_rated_replies_count'   => (int) ($user->highly_rated_replies_count ?? 0),
            ],
        ];
    }

    /**
     * Update profile identity fields.
     */
    public function updateProfile(User $user, array $data): void
    {
        $user->update([
            'name'             => $data['name'],
            'nickname'         => $data['nickname'],
            'profession'       => $data['profession']       ?? null,
            'experience_level' => $data['experience_level'] ?? null,
            'sector'           => $data['sector']           ?? null,
            'bio'              => $data['bio']              ?? null,
        ]);
    }

    /**
     * Update hub memberships.
     */
    public function updateHubs(User $user, array $hubIds): void
    {
        $user->hubs()->sync($hubIds);
    }

    /**
     * Update privacy settings.
     */
    public function updatePrivacy(User $user, array $data): void
    {
        $user->update([
            'show_real_name' => (bool) ($data['show_real_name'] ?? false),
        ]);
    }

    /**
     * Update notification preferences.
     */
    public function updateNotifications(User $user, array $data): void
    {
        $user->update([
            'notify_jobs'      => (bool) ($data['notify_jobs']      ?? true),
            'notify_freelance' => (bool) ($data['notify_freelance']  ?? true),
            'notify_forum'     => (bool) ($data['notify_forum']      ?? true),
            'notify_surveys'   => (bool) ($data['notify_surveys']    ?? true),
            'notify_documents' => (bool) ($data['notify_documents']  ?? true),
        ]);
    }

    /**
     * Change password — verifies current password first.
     *
     * @throws \Exception if current password is wrong
     */
    public function updatePassword(User $user, array $data): void
    {
        if (! Hash::check($data['current_password'], $user->password)) {
            throw new \Exception(__('profile.password_incorrect'));
        }

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