<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Libraries;

use App\Jobs\Notifications;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\BeatmapsetEvent;
use App\Models\User;

class BeatmapsetDiscussionPostNew extends BeatmapsetDiscussionPostHandlesProblem
{
    private Beatmapset $beatmapset;
    private BeatmapDiscussionPost $post;
    private bool $willResolvedChange = false;

    private function __construct(protected User $user, private BeatmapDiscussion $discussion, private string $message)
    {
        $this->beatmapset = $discussion->beatmapset;
        $this->post = $this->discussion->beatmapDiscussionPosts()->make(['message' => $message]);
        $this->post->user()->associate($user);
        $this->post->beatmapDiscussion()->associate($discussion);

        if ($discussion->message_type === 'problem') {
            $this->problemDiscussion = $discussion;
            $this->priorOpenProblemCount = $this->beatmapset->beatmapDiscussions()->openProblems()->count();
        }

        $this->willResolvedChange = $this->discussion->exists
            && $this->discussion->canBeResolved()
            && $this->discussion->isDirty('resolved');
    }

    public static function create(User $user, array $params)
    {
        $discussion = static::prepareDiscussion($user, $params);

        if (!$discussion->exists) {
            priv_check('BeatmapDiscussionStore', $discussion)->ensureCan();
        }

        $postParams = get_params($params, 'beatmap_discussion_post', ['message']);

        return [
            $discussion,
            (new static($user, $discussion, $postParams['message']))->handle(),
        ];
    }

    private static function prepareDiscussion(User $user, array $request): BeatmapDiscussion
    {
        $params = get_params($request, null, [
            'beatmap_discussion_id:int',<commit_msg>should use priv_check_user<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Libraries;

use App\Jobs\Notifications;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\BeatmapsetEvent;
use App\Models\User;

class BeatmapsetDiscussionPostNew extends BeatmapsetDiscussionPostHandlesProblem
{
    private Beatmapset $beatmapset;
    private BeatmapDiscussionPost $post;
    private bool $willResolvedChange = false;

    private function __construct(protected User $user, private BeatmapDiscussion $discussion, private string $message)
    {
        $this->beatmapset = $discussion->beatmapset;
        $this->post = $this->discussion->beatmapDiscussionPosts()->make(['message' => $message]);
        $this->post->user()->associate($user);
        $this->post->beatmapDiscussion()->associate($discussion);

        if ($discussion->message_type === 'problem') {
            $this->problemDiscussion = $discussion;
            $this->priorOpenProblemCount = $this->beatmapset->beatmapDiscussions()->openProblems()->count();
        }

        $this->willResolvedChange = $this->discussion->exists
            && $this->discussion->canBeResolved()
            && $this->discussion->isDirty('resolved');
    }

    public static function create(User $user, array $params)
    {
        $discussion = static::prepareDiscussion($user, $params);

        if (!$discussion->exists) {
            priv_check_user($user, 'BeatmapDiscussionStore', $discussion)->ensureCan();
        }

        $postParams = get_params($params, 'beatmap_discussion_post', ['message']);

        return [
            $discussion,
            (new static($user, $discussion, $postParams['message']))->handle(),
        ];
    }

    private static function prepareDiscussion(User $user, array $request): BeatmapDiscussion
    {
        $params = get_params($request, null, [
            'beatmap_discussion_id:int',
{'fraction_non_alphanumeric': 0.13106221906789686, 'fraction_numerical': 0.0014194464158978, 'mean_word_length': 3.536480686695279, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968405', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1131, 'n_words': 246}