<?php

namespace Database\Factories;

use App\Models\Contract;
use App\Models\Deliverable;
use App\Models\Project;
use App\Models\ProjectFile;
use App\Models\Proposal;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Project>
 */
class ProjectFactory extends Factory
{
    /**
     * Configure the model factory.
     *
     * @return $this
     */
    public function configure()
    {
        return $this->afterCreating(function (Project $project) {
            $project->addMastery(1,3);
            $project->addLocation(
                [
                    'long'=> $this->faker->longitude(31, 31.5),
                    'lat'=> $this->faker->longitude(29.5, 31) ,
                    'address' => 'Cairo, Egypt',
                ]
            );
            $project->categories()->attach([1,2]);
            $project->addCurrency(1);
            $project->files()->save(ProjectFile::factory()->create([
                'project_id'=>$project->id,
            ]));
//            $project->proposals()->save(Proposal::factory()->create([
//                'project_id'=>$project->id,
//                'accepted'=>false,
//            ]));
//
//            if($project->id % 4 == 0){
//                $acceptedProp = $project->proposals()->save(Proposal::factory()->create([
//                        'project_id'=>$project->id,
//                        'accepted'=>true,
//                    ]));
//
//                $contractorSignedAtt = $this->faker->dateTimeBetween('-10 days');
//                $project->status = 'Processing';
//                $project->projectable()->save($acceptedProp->proposalable());
//                $project->save();
//                $project->searchable();
//                $project->contract()->save(Contract::create([
//                    'project_id'=>$project->id,
//                    'user_id'=>$project->user_id,
//                    'contractor_id' => $acceptedProp->proposalable_id,
//                    'contractor_type' => $acceptedProp->proposalable_type,
//                    'amount'=>$acceptedProp->amount,
//                    'paid_amount' => $this->faker->numberBetween(0,$acceptedProp->amount),
//                    'proposal_id' => $acceptedProp->id,
//                    'contractor_signed_at' => $contractorSignedAtt,
//                    'user_signed_at' => $this->faker->dateTimeBetween('-350 days'),
//                    'start_date' => $contractorSignedAtt,
//                    'due_date' => Carbon::createFromTimestamp($contractorSignedAtt->getTimestamp())->addDays($acceptedProp->duration),
//                ]));
//            }
            $project->skills()->attach([1,3]);
        });
    }


    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'user_id'=> User::factory(),
            'title'=> $this->faker->title,
            'description'=> $this->faker->sentence(),
            'category_id'=> $this->faker->numberBetween(1,300),
            'level_of_experience_id'=> $this->faker->numberBetween(1,3),
            'start_date'=> $this->faker->dateTime(),
            'duration'=> $this->faker->numberBetween(1,90),
            'budget_type'=> $this->faker->randomElement(['Fixed Price','Rate','Bidding']),
            'status'=> 'Open',
            'amount'=> $this->faker->numberBetween(1,500),
            'slug'=>$this->faker->uuid(),
        ];
    }
}
