<?php

namespace Tests\Feature\Invoice;

use App\Invoice\BillKind;
use App\Invoice\Models\Invoice;
use App\Member\Member;
use App\Payment\Subscription;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\RequestFactories\Child;
use Tests\TestCase;

class MassStoreActionTest extends TestCase
{
    use DatabaseTransactions;

    public function setUp(): void
    {
        parent::setUp();

        $this->login()->loginNami()->withoutExceptionHandling();
    }

    public function testItDoesntCreatePaymentsWithoutSubscription(): void
    {
        Member::factory()->defaults()->emailBillKind()->create(['subscription_id' => null]);

        $this->postJson(route('invoice.mass-store'), [
            'year' => now()->addYear()->year,
        ])->assertOk();

        $this->assertDatabaseEmpty('invoices');
    }

    public function testItDoesntCreatePaymentWithoutBillKind(): void
    {
        Member::factory()->defaults()->create();

        $this->postJson(route('invoice.mass-store'), [
            'year' => now()->addYear()->year,
        ])->assertOk();

        $this->assertDatabaseEmpty('invoices');
    }

    public function testItCreatesPayments(): void
    {
        $member = Member::factory()->defaults()
            ->for(Subscription::factory()->forFee()->children([
                new Child('beitrag {name}', 4466),
                new Child('beitrag2 für {name} für {year}', 2290),
            ]))->emailBillKind()->create(['firstname' => 'Max', 'lastname' => 'Muster', 'address' => 'Maxstr 4', 'zip' => '33445', 'location' => 'Solingen', 'email' => 'lala@b.de']);

        $this->postJson(route('invoice.mass-store'), [
            'year' => now()->addYear()->year,
        ])->assertOk();

        $invoice = Invoice::first();
        $this->assertNotNull($invoice);
        $this->assertEquals('Familie Muster', $invoice->to->name);
        $this->assertEquals('Maxstr 4', $invoice->to->address);
        $this->assertEquals('33445', $invoice->to->zip);
        $this->assertEquals('Solingen', $invoice->to->location);
        $this->assertEquals('Liebe Familie Muster', $invoice->to->greeting);
        $this->assertEquals('lala@b.de', $invoice->to->email);
        $this->assertEquals('Mitgliedsbeitrag für Muster', $invoice->usage);
        $this->assertEquals(BillKind::EMAIL, $invoice->via);
        $this->assertDatabaseHas('invoice_positions', [
            'invoice_id' => $invoice->id,
            'member_id' => $member->id,
            'price' => 4466,
            'description' => 'beitrag Max Muster'
        ]);
        $this->assertDatabaseHas('invoice_positions', [
            'invoice_id' => $invoice->id,
            'member_id' => $member->id,
            'price' => 2290,
            'description' => 'beitrag2 für Max Muster für ' . now()->addYear()->year
        ]);
    }

    public function testItCreatesOneInvoiceForFamilyMember(): void
    {
        $subscription = Subscription::factory()->forFee()->children([new Child('beitrag {name}', 4466)])->create();
        $member = Member::factory()->defaults()->for($subscription)->emailBillKind()->create(['firstname' => 'Max', 'lastname' => 'Muster']);
        Member::factory()->defaults()->for($subscription)->sameFamilyAs($member)->emailBillKind()->create(['firstname' => 'Jane']);

        $this->postJson(route('invoice.mass-store'), ['year' => now()->addYear()->year])->assertOk();

        $this->assertDatabaseCount('invoices', 1);
        $this->assertDatabaseCount('invoice_positions', 2);
        $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Max Muster', 'price' => 4466]);
        $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Jane Muster', 'price' => 4466]);
    }

    public function testItSeparatesBillKinds(): void
    {
        $subscription = Subscription::factory()->forFee()->children([new Child('beitrag {name]', 4466)])->create();
        $member = Member::factory()->defaults()->for($subscription)->emailBillKind()->create();
        Member::factory()->defaults()->for($subscription)->sameFamilyAs($member)->postBillKind()->create();

        $this->postJson(route('invoice.mass-store'), ['year' => now()->addYear()->year])->assertOk();

        $this->assertDatabaseCount('invoices', 2);
        $this->assertDatabaseCount('invoice_positions', 2);
    }

    public function testItSeparatesSubscriptions(): void
    {
        $member1 = Member::factory()->defaults()->emailBillKind()
            ->for(Subscription::factory()->forFee()->children([new Child('beitrag1 {name}', 4466)]))
            ->create(['firstname' => 'Member1', 'lastname' => 'ln']);
        $member2 = Member::factory()->defaults()->sameFamilyAs($member1)->emailBillKind()
            ->for(Subscription::factory()->forFee()->children([new Child('beitrag2 {name}', 4467)]))
            ->create(['firstname' => 'Member2']);

        $this->postJson(route('invoice.mass-store'), ['year' => now()->addYear()->year])->assertOk();
        $invoice = Invoice::first();

        $this->assertDatabaseCount('invoice_positions', 2);
        $this->assertDatabaseHas('invoice_positions', ['invoice_id' => $invoice->id, 'member_id' => $member1->id, 'description' => 'beitrag1 Member1 ln', 'price' => 4466]);
        $this->assertDatabaseHas('invoice_positions', ['invoice_id' => $invoice->id, 'member_id' => $member2->id, 'description' => 'beitrag2 Member2 ln', 'price' => 4467]);
    }
}
