<?php

namespace App\Services;

use App\Models\User;

class UserPreferenceService
{
    public function __construct(
        protected SystemSettingService $systemSettingService
    ) {
    }

    /**
     * @return array{
     *   inbox_enabled:bool,
     *   popup_enabled:bool,
     *   email_admin_updates:bool,
     *   email_transaction_updates:bool,
     *   marketing_opt_in:bool
     * }
     */
    public function notificationPreferences(User|int $user): array
    {
        $values = $this->systemSettingService->many(
            $this->section($user),
            $this->notificationDefaults()
        );

        return [
            'inbox_enabled' => (bool) ($values['inbox_enabled'] ?? true),
            'popup_enabled' => (bool) ($values['popup_enabled'] ?? true),
            'email_admin_updates' => (bool) ($values['email_admin_updates'] ?? true),
            'email_transaction_updates' => (bool) ($values['email_transaction_updates'] ?? true),
            'marketing_opt_in' => (bool) ($values['marketing_opt_in'] ?? false),
        ];
    }

    /**
     * @param  array{
     *   inbox_enabled?:bool,
     *   popup_enabled?:bool,
     *   email_admin_updates?:bool,
     *   email_transaction_updates?:bool,
     *   marketing_opt_in?:bool
     * }  $data
     * @return array{
     *   inbox_enabled:bool,
     *   popup_enabled:bool,
     *   email_admin_updates:bool,
     *   email_transaction_updates:bool,
     *   marketing_opt_in:bool
     * }
     */
    public function updateNotificationPreferences(User|int $user, array $data): array
    {
        $merged = array_merge($this->notificationPreferences($user), $data);
        $section = $this->section($user);

        $this->systemSettingService->putMany([
            ['section' => $section, 'key' => 'inbox_enabled', 'value' => (bool) ($merged['inbox_enabled'] ?? true), 'type' => 'boolean'],
            ['section' => $section, 'key' => 'popup_enabled', 'value' => (bool) ($merged['popup_enabled'] ?? true), 'type' => 'boolean'],
            ['section' => $section, 'key' => 'email_admin_updates', 'value' => (bool) ($merged['email_admin_updates'] ?? true), 'type' => 'boolean'],
            ['section' => $section, 'key' => 'email_transaction_updates', 'value' => (bool) ($merged['email_transaction_updates'] ?? true), 'type' => 'boolean'],
            ['section' => $section, 'key' => 'marketing_opt_in', 'value' => (bool) ($merged['marketing_opt_in'] ?? false), 'type' => 'boolean'],
        ]);

        return $this->notificationPreferences($user);
    }

    /**
     * @return array{
     *   inbox_enabled:bool,
     *   popup_enabled:bool,
     *   email_admin_updates:bool,
     *   email_transaction_updates:bool,
     *   marketing_opt_in:bool
     * }
     */
    protected function notificationDefaults(): array
    {
        return [
            'inbox_enabled' => true,
            'popup_enabled' => true,
            'email_admin_updates' => true,
            'email_transaction_updates' => true,
            'marketing_opt_in' => false,
        ];
    }

    protected function section(User|int $user): string
    {
        $userId = $user instanceof User ? (int) $user->id : (int) $user;

        return "user.preferences.{$userId}";
    }
}

