<?php

namespace Database\Seeders;

use App\Models\DeliveryPrice;
use App\Models\Power;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use PhpOffice\PhpSpreadsheet\IOFactory;

class Logistics extends Seeder
{
    private $last_power = 0;
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        Power::truncate();
        $this->call([
            PowerSeeder::class,
        ]);
        DeliveryPrice::truncate();

        // Define the path to your XLSX file in the public folder
        $filePath = public_path('logistics.xlsx'); // Update the path
        // Load the spreadsheet
        $spreadsheet = IOFactory::load($filePath);
        // Get the active sheet
        $sheet = $spreadsheet->getActiveSheet();

        // Convert the sheet to an array
        $data = $sheet->toArray();

        foreach ($data as $index => $row) {
            if ($index > 1) {
                $toshkent_shahar_14 = $data[1][3];
                $toshkent_viloyati_11 = $data[1][4];
                $fargona_12 = $data[1][5];
                $andijon_2 = $data[1][6];
                $namangan_7 = $data[1][7];
                $sirdaryo_10 = $data[1][8];
                $jizzax_4 = $data[1][9];
                $samarqand_8 = $data[1][10];
                $navoiy_6 = $data[1][11];
                $buxoro_3 = $data[1][12];
                $qashqadaryo_5 = $data[1][13];
                $surxondaryo_9 = $data[1][14];
                $xorazm_13 = $data[1][15];
                $qoraqalpogiston_1 = $data[1][16];

                $power_value_in_kW = $row[1];

                if ($power_value_in_kW == null) {
                    $power_value_in_kW = $this->last_power;
                } else {
                    $this->last_power = $power_value_in_kW;
                }

                $power = $this->getPower($power_value_in_kW);

                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 14, // Toshkent shahar
                    'price' => $row[3] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 11, // Toshkent viloyati
                    'price' => $row[4] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 12, // Farg'ona
                    'price' => $row[5] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 2, // Andijon
                    'price' => $row[6] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 7, // Namangan
                    'price' => $row[7] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 10, // Sirdaryo
                    'price' => $row[8] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 4, // Jizzax
                    'price' => $row[9] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 8, // Samarqand
                    'price' => $row[10] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 6, // Navoiy
                    'price' => $row[11] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 3, // Buxoro
                    'price' => $row[12] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 5, // Qashqadaryo
                    'price' => $row[13] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 9, // Surxondaryo
                    'price' => $row[14] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 13, // Xorazm
                    'price' => $row[15] * 1_000_000,
                ]);
                DeliveryPrice::create([
                    'power_id' => $power->id,
                    'region_id' => 1, // Qoraqalpog'iston
                    'price' => $row[16] * 1_000_000,
                ]);
            }
        }
    }

    private function getPower($value)
    {
        if ($value != null) {
            $power = Power::where('power', $value * 1000)->first();
            if (!$power) {
                $power = Power::create([
                    'name' => [
                        'uz' => $value * 1000 . ' kW',
                        'ru' => $value * 1000 . ' кВт',
                    ],
                    'power' => $value,
                ]);
            }

            return $power;
        }
    }
}
