<?php

/*
 * This file is part of Laravel Love.
 *
 * (c) Anton Komarev <anton@komarev.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Cog\Tests\Laravel\Love\Unit\Reacter\Models;

use Cog\Contracts\Love\Reactant\Exceptions\ReactantInvalid;
use Cog\Contracts\Love\Reacter\Exceptions\NotAssignedToReacterable;
use Cog\Contracts\Love\Reaction\Exceptions\RateInvalid;
use Cog\Contracts\Love\Reaction\Exceptions\ReactionAlreadyExists;
use Cog\Contracts\Love\Reaction\Exceptions\ReactionNotExists;
use Cog\Laravel\Love\Reactant\Models\NullReactant;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\Reacter\Models\NullReacter;
use Cog\Laravel\Love\Reacter\Models\Reacter;
use Cog\Laravel\Love\Reaction\Models\Reaction;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Tests\Laravel\Love\Stubs\Models\Article;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
use Cog\Tests\Laravel\Love\TestCase;
use TypeError;

final class ReacterTest extends TestCase
{
    /** @test */
    public function it_can_fill_type(): void
    {
        $reacter = new Reacter([
            'type' => 'TestType',
        ]);

        $this->assertSame('TestType', $reacter->getAttribute('type'));
    }

    /** @test */
    public function it_casts_id_to_string(): void
    {
        $reacter = Reacter::factory()->make([
            'id' => 4,
        ]);

        $this->assertSame('4', $reacter->getAttribute('id'));
        $this->assertSame('4', $reacter->getId());
    }

    /** @test */
    public function it_can_morph_to_reacterable(): void
    {
        $reacter = Reacter::factory()->create([
            'type' => (new User())->getMorphClass(),
        ]);

        $reacterable = User::factory()->create([
            'love_reacter_id' => $reacter->getId(),
        ]);

        $this->assertTrue($reacter->reacterable->is($reacterable));
    }

    /** @test */
    public function it_can_has_reaction(): void
    {
        $reacter = Reacter::factory()->create();

        $reaction = Reaction::factory()->create([
            'reacter_id' => $reacter->getId(),
        ]);

        $assertReaction = $reacter->reactions->first();
        $this->assertTrue($assertReaction->is($reaction));
    }

    /** @test */
    public function it_can_has_many_reactions(): void
    {
        $reacter = Reacter::factory()->create();

        $reactions = Reaction::factory()->count(2)->create([
            'reacter_id' => $reacter->getId(),
        ]);

        $assertReactions = $reacter->reactions;
        $this->assertTrue($assertReactions->get(0)->is($reactions->get(0)));
        $this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
    }

    /** @test */
    public function it_can_get_id(): void
    {
        $reacter = Reacter::factory()->make([
            'id' => '4',
        ]);

        $this->assertSame('4', $reacter->getId());
    }

    /** @test */
    public function it_throws_exception_on_get_id_when_id_is_null(): void
    {
        $this->expectException(TypeError::class);

        $reacter = new Reacter();

        $reacter->getId();
    }

    /** @test */
    public function it_can_get_reacterable(): void
    {
        $reacter = Reacter::factory()->create([
            'type' => (new User())->getMorphClass(),
        ]);

        $reacterable = User::factory()->create([
            'love_reacter_id' => $reacter->getId(),
        ]);

        $this->assertTrue($reacter->getReacterable()->is($reacterable));
    }

    /** @test */
    public function it_can_throw_exception_on_get_reacterable_when_not_assigned_to_any_reacterable(): void
    {
        $this->expectException(NotAssignedToReacterable::class);

        $reacter = Reacter::factory()->create([
            'type' => (new User())->getMorphClass(),
        ]);

        $reacter->getReacterable();
    }

    /** @test */
    public function it_can_get_reactions(): void
    {
        $reacter = Reacter::factory()->create();

        $reactions = Reaction::factory()->count(2)->create([
            'reacter_id' => $reacter->getId(),
        ]);

        $assertReactions = $reacter->getReactions();
        $this->assertTrue($assertReactions->get(0)->is($reactions->get(0)));
        $this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
    }

    /** @test */
    public function it_can_react_to_reactant_without_rate(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactable = Article::factory()->create();
        $reactant = $reactable->loveReactant;

        $reacter->reactTo($reactant, $reactionType);

        $this->assertCount(1, $reacter->reactions);
        $assertReaction = $reacter->reactions->first();
        $this->assertTrue($assertReaction->reactant->is($reactant));
        $this->assertSame(1.0, $assertReaction->rate);
    }

    /** @test */
    public function it_can_react_to_reactant_which_reacter_too(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactable = User::factory()->create();
        $reactant = $reactable->loveReactant;

        $reacter->reactTo($reactant, $reactionType);

        $this->assertCount(1, $reacter->reactions);
        $assertReaction = $reacter->reactions->first();
        $this->assertTrue($assertReaction->reactant->is($reactant));
    }

    /** @test */
    public function it_can_react_to_reactable_with_rate(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();

        $reacter->reactTo($reactant, $reactionType, 2.0);

        $this->assertCount(1, $reacter->reactions);
        $assertReaction = $reacter->reactions->first();
        $this->assertSame(2.0, $assertReaction->rate);
    }

    /** @test */
    public function it_can_change_reaction_rate_with_react_to_when_reaction_already_exists(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();

        $reacter->reactTo($reactant, $reactionType);
        $reacter->reactTo($reactant, $reactionType, 2.0);

        $this->assertCount(1, $reacter->reactions);
        $assertReaction = $reacter->reactions->first();
        $this->assertSame(2.0, $assertReaction->rate);
    }

    /** @test */
    public function it_throws_rate_invalid_on_react_to_when_reaction_already_exists_with_same_rate(): void
    {
        $this->expectException(RateInvalid::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();

        $reacter->reactTo($reactant, $reactionType, 2.0);
        $reacter->reactTo($reactant, $reactionType, 2.0);
    }

    /** @test */
    public function it_throws_reactant_invalid_on_react_to_when_reactant_is_null_object(): void
    {
        $this->expectException(ReactantInvalid::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $reacter->reactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_throws_reactant_invalid_on_react_to_when_reactant_is_not_persisted(): void
    {
        $this->expectException(ReactantInvalid::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $reacter->reactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_throws_reaction_already_exists_when_react_to_creating_duplicate(): void
    {
        $this->expectException(ReactionAlreadyExists::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();

        $reacter->reactTo($reactant, $reactionType);
        $reacter->reactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_can_unreact_to_reactant(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        $reaction = Reaction::factory()->create([
            'reaction_type_id' => $reactionType,
            'reactant_id' => $reactant->getId(),
            'reacter_id' => $reacter->getId(),
        ]);

        $reacter->unreactTo($reactant, $reactionType);

        $this->assertCount(0, $reacter->reactions);
        $this->assertFalse($reaction->exists());
    }

    /** @test */
    public function it_throws_reactant_invalid_on_unreact_to_when_reactant_is_null_object(): void
    {
        $this->expectException(ReactantInvalid::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $reacter->unreactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_throws_reactant_invalid_on_unreact_to_when_reactant_is_not_persisted(): void
    {
        $this->expectException(ReactantInvalid::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $reacter->unreactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_cannot_unreact_reactant_if_not_reacted(): void
    {
        $this->expectException(ReactionNotExists::class);

        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType,
            'reacter_id' => $reacter->getId(),
        ]);
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType,
            'reactant_id' => $reactant->getId(),
        ]);

        $reacter->unreactTo($reactant, $reactionType);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
        ]);

        $isReacted = $reacter->hasReactedTo($reactant);

        $this->assertTrue($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_when_reactant_is_null_object(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isReacted = $reacter->hasReactedTo($reactant);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_when_reactant_is_not_persisted(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isReacted = $reacter->hasReactedTo($reactant);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reacter_id' => $reacter->getId(),
        ]);
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reactant_id' => $reactant->getId(),
        ]);

        $isNotReacted = $reacter->hasNotReactedTo($reactant);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_when_reactant_is_null_object(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isNotReacted = $reacter->hasNotReactedTo($reactant);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_when_reactant_is_not_persisted(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isNotReacted = $reacter->hasNotReactedTo($reactant);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
        ]);

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType);

        $this->assertTrue($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type_when_reactant_is_null_object(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type_when_reactant_is_not_persisted(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type(): void
    {
        $reactionType = ReactionType::factory()->create();
        $otherReactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $otherReactionType->getId(),
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
        ]);
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reactant_id' => $reactant->getId(),
        ]);

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type_when_reactant_is_null_object(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type_when_reactant_is_not_persisted(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_rate(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
            'rate' => 2.0,
        ]);

        $isReacted = $reacter->hasReactedTo($reactant, null, 2.0);

        $this->assertTrue($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_rate_when_reactant_is_null_object(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isReacted = $reacter->hasReactedTo($reactant, null, 2.0);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_rate_when_reactant_is_not_persisted(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isReacted = $reacter->hasReactedTo($reactant, null, 2.0);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_rate(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
            'rate' => 2.2,
        ]);
        Reaction::factory()->create([
            'reactant_id' => $reactant->getId(),
            'rate' => 2.0,
        ]);

        $isNotReacted = $reacter->hasNotReactedTo($reactant, null, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_rate_when_reactant_is_null_object(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isNotReacted = $reacter->hasNotReactedTo($reactant, null, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_rate_when_reactant_is_not_persisted(): void
    {
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isNotReacted = $reacter->hasNotReactedTo($reactant, null, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type_and_rate(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
            'rate' => 2.0,
        ]);

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType, 2.0);

        $this->assertTrue($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type_and_rate_when_reactant_is_null_object(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType, 2.0);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_reacted_to_reactant_with_type_and_rate_when_reactant_is_not_persisted(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isReacted = $reacter->hasReactedTo($reactant, $reactionType, 2.0);

        $this->assertFalse($isReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type_and_rate(): void
    {
        $reactionType = ReactionType::factory()->create();
        $otherReactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::factory()->create();
        Reaction::factory()->create([
            'reaction_type_id' => $otherReactionType->getId(),
            'reacter_id' => $reacter->getId(),
            'reactant_id' => $reactant->getId(),
            'rate' => 2.2,
        ]);
        Reaction::factory()->create([
            'reaction_type_id' => $reactionType->getId(),
            'reactant_id' => $reactant->getId(),
            'rate' => 2.0,
        ]);

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type_and_rate_when_reactant_is_null_object(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = new NullReactant(new Article());

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_has_not_reacted_to_reactant_with_type_and_rate_when_reactant_is_not_persisted(): void
    {
        $reactionType = ReactionType::factory()->create();
        $reacter = Reacter::factory()->create();
        $reactant = Reactant::query()->make();

        $isNotReacted = $reacter->hasNotReactedTo($reactant, $reactionType, 2.0);

        $this->assertTrue($isNotReacted);
    }

    /** @test */
    public function it_can_check_is_equal_to_self(): void
    {
        $reacter = Reacter::factory()->create();

        $this->assertTrue($reacter->isEqualTo($reacter));
    }

    /** @test */
    public function it_can_check_is_equal_to_other_reacter(): void
    {
        $reacter = Reacter::factory()->create();
        $otherReacter = Reacter::factory()->create();

        $this->assertFalse($reacter->isEqualTo($otherReacter));
    }

    /** @test */
    public function it_can_check_is_equal_to_null_object_reacter(): void
    {
        $reacter = Reacter::factory()->create();
        $nullReacter = new NullReacter(new User());

        $this->assertFalse($reacter->isEqualTo($nullReacter));
    }

    /** @test */
    public function it_can_check_is_equal_to_null_object_reacter_when_not_null_object_not_persisted(): void
    {
        $reacter = new Reacter();
        $nullReacter = new NullReacter(new User());

        $this->assertFalse($reacter->isEqualTo($nullReacter));
    }

    /** @test */
    public function it_can_check_is_not_equal_to_self(): void
    {
        $reacter = Reacter::factory()->create();

        $this->assertFalse($reacter->isNotEqualTo($reacter));
    }

    /** @test */
    public function it_can_check_is_not_equal_to_other_reacter(): void
    {
        $reacter = Reacter::factory()->create();
        $otherReacter = Reacter::factory()->create();

        $this->assertTrue($reacter->isNotEqualTo($otherReacter));
    }

    /** @test */
    public function it_can_check_is_not_equal_to_null_object_reacter(): void
    {
        $reacter = Reacter::factory()->create();
        $nullReacter = new NullReacter(new User());

        $this->assertTrue($reacter->isNotEqualTo($nullReacter));
    }

    /** @test */
    public function it_can_check_is_not_equal_to_null_object_reacter_when_not_null_object_not_persisted(): void
    {
        $reacter = new Reacter();
        $nullReacter = new NullReacter(new User());

        $this->assertTrue($reacter->isNotEqualTo($nullReacter));
    }

    /** @test */
    public function it_can_check_is_null(): void
    {
        $reacter = Reacter::factory()->create();

        $this->assertFalse($reacter->isNull());
    }

    /** @test */
    public function it_can_check_is_null_when_reacter_not_persisted(): void
    {
        $reacter = new Reacter();

        $this->assertTrue($reacter->isNull());
    }

    /** @test */
    public function it_can_check_is_not_null(): void
    {
        $reacter = Reacter::factory()->create();

        $this->assertTrue($reacter->isNotNull());
    }

    /** @test */
    public function it_can_check_is_not_null_when_reacter_not_persisted(): void
    {
        $reacter = new Reacter();

        $this->assertFalse($reacter->isNotNull());
    }
}
