<?php

namespace Database\Factories;

use App\Models\Wine;
use App\Models\GrapeVariety;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Wine>
 */
class WineFactory extends Factory
{
    protected $model = Wine::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $wineTypes = ['white', 'red', 'rose'];
        $statuses = ['in_production', 'aging', 'ready', 'sold_out'];
        $bottlesProduced = fake()->numberBetween(300, 3000);
        $bottlesSold = fake()->numberBetween(0, $bottlesProduced / 2);

        $wineNames = [
            'Reserve Selection',
            'Estate Classic',
            'Late Harvest',
            'Premium Vintage',
            'Grand Cru',
            'Special Edition',
            'Limited Release',
            'Heritage Collection'
        ];

        return [
            'vintage' => fake()->numberBetween(2018, 2024),
            'grape_variety_id' => GrapeVariety::factory(),
            'wine_name' => fake()->randomElement($wineNames) . ' ' . fake()->year(),
            'wine_type' => fake()->randomElement($wineTypes),
            'description' => fake()->paragraph(2),
            'alcohol_percentage' => fake()->randomFloat(2, 10.0, 14.5),
            'bottles_produced' => $bottlesProduced,
            'bottles_in_stock' => $bottlesProduced - $bottlesSold,
            'price_per_bottle' => fake()->randomFloat(2, 150.0, 500.0),
            'bottle_volume' => fake()->randomElement([0.375, 0.75, 1.5]),
            'production_date' => fake()->dateTimeBetween('-2 years', '-6 months'),
            'bottling_date' => fake()->dateTimeBetween('-6 months', 'now'),
            'status' => fake()->randomElement($statuses),
            'image_url' => fake()->optional()->imageUrl(640, 480, 'wine', true),
        ];
    }

    /**
     * Indicate that the wine is ready for sale.
     */
    public function ready(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'ready',
        ]);
    }

    /**
     * Indicate that the wine is sold out.
     */
    public function soldOut(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'sold_out',
            'bottles_in_stock' => 0,
        ]);
    }

    /**
     * Indicate that the wine is aging.
     */
    public function aging(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'aging',
        ]);
    }
}
