<?php

namespace Database\Factories\Goliath;


use App\Models\Goliath\GoliathAppliance;
use App\Models\Goliath\GoliathAttack;
use App\Models\Goliath\GoliathAttackCollected;
use Illuminate\Database\Eloquent\Factories\Factory;
use Carbon\Carbon;
use Illuminate\Support\Str;

class GoliathAttackFactory extends Factory
{
    protected $model = GoliathAttack::class;

    public function definition(): array
    {

        return [
            'region_id'             => null,
            'subregion_id'          => null,
            'metro_id'              => null,
            'appliance_id'          => null,
            'type'                  => Str::random('8'),
            'description'           => Str::random('8'),
            'destination'           => null,
            'started_at'            => null,
            'last_at'               => null
        ];
    }

    public function id(int $id): static
    {
        return $this->state(function (array $attributes) use($id) {
            return [
                'id'=> $id
            ];
        });
    }

    public function start(Carbon $start): static
    {
        return $this->state(function (array $attributes) use($start) {
            return [
                'started_at'            => $start
            ];
        });
    }

    public function finish(Carbon $finish): static
    {
        return $this->state(function (array $attributes) use($finish) {
            return [
                'last_at'            => $finish
            ];
        });
    }

    public function address(string $address): static
    {
        return $this->state(function (array $attributes) use ($address) {
            return [
                'destination' => $address
            ];
        });
    }

    public function appliance(GoliathAppliance $appliance): static
    {
        return $this->state(function (array $attributes) use ($appliance) {
            return [
                'region_id'             => $appliance->region_id,
                'subregion_id'          => $appliance->subregion_id,
                'metro_id'              => $appliance->metro_id,
                'appliance_id'          => $appliance->id,
            ];
        });
    }

    public function collected(): static
    {
        return $this->afterCreating(function (GoliathAttack $x) {
            GoliathAttackCollected::create([ 'attack_id' => $x->id ]);
        });
    }

}