<?php

namespace App\Actions\Games;

use App\Models\Game;
use App\Models\GameCharacter;
use App\Models\GamePlatform;
use App\Models\GameRank;
use App\Models\GameRegion;
use App\Models\GameRole;
use App\Models\GameServer;
use Illuminate\Validation\ValidationException;

trait ValidatesGameCatalogSelections
{
    protected function validateGameCatalogSelections(Game $game, array $data): void
    {
        $errors = [];

        if (filled($data['current_rank_id'] ?? null) && ! $game->ranks()->whereKey($data['current_rank_id'])->exists()) {
            $errors['current_rank_id'] = __('validation.exists', ['attribute' => 'current rank']);
        }

        if (filled($data['highest_rank_id'] ?? null) && ! $game->ranks()->whereKey($data['highest_rank_id'])->exists()) {
            $errors['highest_rank_id'] = __('validation.exists', ['attribute' => 'highest rank']);
        }

        if (filled($data['primary_platform_id'] ?? null) && ! $game->platforms()->whereKey($data['primary_platform_id'])->exists()) {
            $errors['primary_platform_id'] = __('validation.exists', ['attribute' => 'primary platform']);
        }

        if (filled($data['region_id'] ?? null) && ! $game->regions()->whereKey($data['region_id'])->exists()) {
            $errors['region_id'] = __('validation.exists', ['attribute' => 'region']);
        }

        if (filled($data['server_id'] ?? null) && ! $game->servers()->whereKey($data['server_id'])->exists()) {
            $errors['server_id'] = __('validation.exists', ['attribute' => 'server']);
        }

        $roleIds = collect($data['role_ids'] ?? [])->filter()->unique()->values();
        if ($roleIds->isNotEmpty()) {
            $validRoleCount = GameRole::query()
                ->where('game_id', $game->id)
                ->whereIn('id', $roleIds)
                ->count();

            if ($validRoleCount !== $roleIds->count()) {
                $errors['role_ids'] = __('validation.exists', ['attribute' => 'roles']);
            }
        }

        $characterIds = collect($data['character_ids'] ?? [])->filter()->unique()->values();
        if ($characterIds->isNotEmpty()) {
            $validCharacterCount = GameCharacter::query()
                ->where('game_id', $game->id)
                ->whereIn('id', $characterIds)
                ->count();

            if ($validCharacterCount !== $characterIds->count()) {
                $errors['character_ids'] = __('validation.exists', ['attribute' => 'characters']);
            }
        }

        $platformIds = collect($data['platform_ids'] ?? [])->filter()->unique()->values();
        if ($platformIds->isNotEmpty()) {
            $validPlatformCount = GamePlatform::query()
                ->where('game_id', $game->id)
                ->whereIn('id', $platformIds)
                ->count();

            if ($validPlatformCount !== $platformIds->count()) {
                $errors['platform_ids'] = __('validation.exists', ['attribute' => 'platforms']);
            }
        }

        if ($errors !== []) {
            throw ValidationException::withMessages($errors);
        }
    }
}
