<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class AmenitiesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $amenities = $this->readCSV(__DIR__ . '/../../resources/csv/amenities.csv',"r");

        \App\Models\PropertyAmenity::truncate();
        
        foreach ($amenities as $key => $amenity) {
            if($key == 0){
                continue;
            }
            \App\Models\PropertyAmenity::create([
                'name' => $amenity[0],
                'name_ar' => $amenity[0],
                'status' => 1,
            ]);
        }

        echo "Amenities Seeder Completed\n";
    }

    function readCSV($csvFile){
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle)) {
            $line = fgetcsv($file_handle, 0);
            if ($line !== false) {
                $line_of_text[] = $line;
            }
        }
        fclose($file_handle);
        return $line_of_text;
    }

    function findKeyForRecord($array, $searchValue, $index) {
        foreach ($array as $key => $value) {
            if ($value[$index] == $searchValue) {
                return $value;
            }
        }
        return null; // Return null if the value is not found
    }
}
