<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Order;
use App\Models\Producer;
use App\Models\Product;
use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Mortezaa97\Regions\Models\County;
use Mortezaa97\Regions\Models\Province;

class OrdersTableSeeder extends Seeder
{
    public function run(): void
    {
        $adminId = User::role('admin')->first()?->id;
        $user = User::role('user')->first();
        $producer = Producer::first();
        $product = Product::first();
        $province = Province::first();
        $county = County::where('province_id', $province?->id)->first();

        if (! $user || ! $product) {
            return;
        }

        Order::firstOrCreate(
            ['order_number' => 'ORD-' . date('Ymd') . '-001'],
            [
                'user_id' => $user->id,
                'producer_id' => $producer?->id,
                'product_id' => $product->id,
                'type' => 'order',
                'quantity' => 1,
                'unit' => 'راس',
                'unit_price' => $product->price,
                'total_price' => $product->price,
                'order_date' => now(),
                'status' => 'pending',
                'province_id' => $province?->id,
                'county_id' => $county?->id,
                'delivery_address' => 'آدرس تحویل سفارش',
                'created_by' => $adminId,
                'updated_by' => $adminId,
            ]
        );
    }
}
