<?php

namespace Database\Seeders;

use App\Models\Claim\Beneficiary;
use App\Models\Claim\Claim;
use App\Models\Claim\TerminalIllness;
use App\Models\Client\Client;
use App\ValueObjects\Postcode;
use Illuminate\Database\Seeder;

class ClientSeeder extends Seeder {
    /**
     * Run the database seeds.
     */
    public function run(): void {
        Client::withoutEvents(function () {

            Client::factory()
                ->count(100)
                ->create()
                ->each(function (Client $client) {

                    $claim = $client->claim()->create(
                        Claim::factory()->make([
                            'solicitor_id' => $client->solicitor_id,
                        ])->toArray()
                    );

                    if (fake()->boolean()) {
                        $claim->beneficiary()->create(
                            Beneficiary::factory()->make([
                                'solicitor_id' => $claim->solicitor_id,
                                'postcode' => new Postcode('SK76BG'),
                            ])->toArray()
                        );
                    }

                    if (fake()->boolean(20)) {
                        $claim->terminalIllness()->create(
                            TerminalIllness::factory()->make([
                                'solicitor_id' => $claim->solicitor_id,
                            ])->toArray()
                        );
                    }

                    $claim->careSettings()->createMany(
                        collect(range(1, rand(1, 3)))
                            ->map(function () use ($claim) {
                                return [
                                    'institution_id' => fake()->numberBetween(1, 10),
                                    'dates' => fake()->dateTimeBetween('-3 years', '-1 year')->format('d/m/Y') . " - " . fake()->dateTimeBetween('-1 year', 'now')->format('d/m/Y'),
                                    'people_involved' => fake()->name() . ', ' . fake()->name(),
                                    'statement' => fake()->paragraph(3),
                                ];
                            })
                            ->toArray()
                    );
                });
        });

    }
}
