<?php

namespace Database\Factories;

use App\Models\DeliverableFile;
use App\Models\Proposal;
use App\Models\ProposalFile;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Proposal>
 */
class ProposalFactory extends Factory
{
    /**
     * Configure the model factory.
     *
     * @return $this
     */
    public function configure()
    {
        return $this->afterCreating(function (Proposal $proposal) {
            $proposal->files()->saveMany(ProposalFile::factory(4)->create([
                'proposal_id' => $proposal->id,
            ]));

            $milestone = $proposal->milestones()->create([
                'name' => 'milestone 1',
                'amount' => '5000',
                'duration' => 75,
                'start_date' => $this->faker->dateTimeBetween('now','+30 days'),
                'due_date' => $this->faker->dateTimeBetween('now','+30 days'),
                'accepted_at' => $proposal->id%10==0 ? $this->faker->dateTimeBetween('now','+30 days') : null,
            ]);

            $milestone->deliverables()->create([
                'name' => '3D max files',
            ]);

            $milestone->files()->saveMany(DeliverableFile::factory(4)->create([
                'milestone_id' => $milestone->id,
            ]));

            $milestone = $proposal->milestones()->create([
                'name' => 'milestone 2',
                'amount' => '2000',
                'duration' => 25,
            ]);

            $milestone->deliverables()->create([
                'name' => 'Low poly files',
            ]);

            $milestone->files()->saveMany(DeliverableFile::factory(4)->create([
                'milestone_id' => $milestone->id,
            ]));
        });
    }

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'cover_letter' => $this->faker->paragraph,
            'description' => $this->faker->paragraph,
            'amount' => $this->faker->numberBetween(0, 50000),
            'duration' => $this->faker->numberBetween(20, 120),
            'project_id' => $this->faker->numberBetween(1, 5),
            'proposalable_id' => $this->faker->numberBetween(1, 5),
            'proposalable_type' => $this->faker->randomElement(['App\Models\Engineer', 'App\Models\Firm']),
            'status' => $this->faker->randomElement(['Pending Approval','Accepted','Declined']),
            'per_project'=>$this->faker->boolean(),
        ];
    }
}
