<?php

namespace Database\Seeders;

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

use App\Models\Admin\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
use App\Models\Admin\FormFieldTypes;
use App\Models\Admin\Taxonomy;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class DatabaseSeeder extends Seeder
{
    private $permissions = [
        'user.create', 'user.read', 'user.update', 'user.delete',
        'post.create', 'post.read', 'post.update', 'post.delete',
        'banner.create', 'banner.read', 'banner.update', 'banner.delete',
        'language.create', 'language.read', 'language.update',
        'settings.read', 'settings.update',
        'taxonomy.create', 'taxonomy.read', 'taxonomy.update', 'taxonomy.delete',
        'filemanager.read',
        'navigation.create', 'navigation.read', 'navigation.update', 'navigation.delete',
        'link.create', 'link.read', 'link.update', 'link.delete',
        'page.create', 'page.read', 'page.update', 'page.delete',
        'popup.create', 'popup.read', 'popup.update', 'popup.delete',
        'product.create', 'product.read', 'product.update', 'product.delete',
    ];

    private $roles = [
        'super-admin' => ['permissions' => 'all',],

        'admin' => ['permissions' => ['user.create','user.read','user.update','user.delete','post.read', 'post.create', 'post.update', 'post.delete', 'banner.read', 'banner.create', 'banner.update', 'banner.delete', 'language.create' ,'language.read', 'language.update', 'settings.read', 'settings.update', 'taxonomy.create', 'taxonomy.read', 'taxonomy.update', 'taxonomy.delete', 'filemanager.read', 'navigation.create', 'navigation.read', 'navigation.update', 'navigation.delete', 'link.create', 'link.read', 'link.update', 'link.delete', 'page.create', 'page.read', 'page.update', 'page.delete', 'popup.create', 'popup.read', 'popup.update', 'popup.delete', 'product.create', 'product.read', 'product.update', 'product.delete', ],],

        'editor' => ['permissions' => ['post.read', 'post.create', 'post.update'],],

        'user' => ['permissions' => [],],
    ];

    public function run(): void
    {
        $user = User::factory()->create([
            'name' => 'Emre',
            'email' => 'emre@tigonproject.com',
        ]);

        $user2 = User::factory()->create([
            'name' => 'Kadir',
            'email' => 'kadir@tigonproject.com',
        ]);

        $user3 = User::factory()->create([
            'name' => 'Emre3',
            'email' => 'emre3@tigonproject.com',
        ]);

        $user4 = User::factory()->create([
            'name' => 'Emre4',
            'email' => 'emre4@tigonproject.com',
        ]);

        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        foreach ($this->permissions as $permission) {
            Permission::create(['name' => $permission]);
        }

        foreach ($this->roles as $roleName => $roleData) {
            $role = Role::create(['name' => $roleName]);
            if ($roleData['permissions'] === 'all') {
                $role->givePermissionTo(Permission::all());
            } else {
                $role->givePermissionTo($roleData['permissions']);
            }
        }

        $user->assignRole('super-admin');
        $user2->assignRole('editor');
        $user3->assignRole('user');
        $user4->assignRole('super-admin');

        $country_path = 'public/sql/languages.sql';
        DB::unprepared(file_get_contents($country_path));
        $this->command->info('Languages table seeded!');

        DB::table('settings')->insert([ 'id' => 1, ]);
        DB::table('seo_settings')->insert([ 'id' => 1, ]);

        FormFieldTypes::create(['name' => 'Text', 'input_type' => 'text']);
        FormFieldTypes::create(['name' => 'Textarea', 'input_type' => 'textarea']);
        FormFieldTypes::create(['name' => 'Select', 'input_type' => 'select']);
        FormFieldTypes::create(['name' => 'Checkbox', 'input_type' => 'checkbox']);
        FormFieldTypes::create(['name' => 'Radio', 'input_type' => 'radio']);
        FormFieldTypes::create(['name' => 'File', 'input_type' => 'file']);
        FormFieldTypes::create(['name' => 'Date', 'input_type' => 'date']);
        FormFieldTypes::create(['name' => 'Hidden', 'input_type' => 'hidden']);
        FormFieldTypes::create(['name' => 'Submit', 'input_type' => 'submit']);

        $country_path = 'public/locations/countries.sql';
        DB::unprepared(file_get_contents($country_path));
        $this->command->info('Country table seeded!');

        $country_path = 'public/locations/cities.sql';
        DB::unprepared(file_get_contents($country_path));
        $this->command->info('City table seeded!');

        //Dikkan Product Categories

        // Main Taxonomy
$taxonomyProduct = Taxonomy::create([
    'name' => ['tr' => 'Ürünler', 'en' => 'Products'],
    'slug' => ['tr' => 'urunler', 'en' => 'products'],
    'handle' => 'products',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyVariants = Taxonomy::create([
    'name' => ['tr' => 'Varyantlar', 'en' => 'Variants'],
    'slug' => ['tr' => 'varyantlar', 'en' => 'variants'],
    'handle' => 'varyantlar',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyIcons = Taxonomy::create([
    'name' => ['tr' => 'İkonlar', 'en' => 'Icons'],
    'slug' => ['tr' => 'ikonlar', 'en' => 'icons'],
    'handle' => 'icons',
    'isSingleSelect' => 2,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

// Products
$taxonomyCategory = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Kategori', 'en' => 'Category'],
    'slug' => ['tr' => 'kategori', 'en' => 'category'],
    'handle' => 'category',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyType = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Tip', 'en' => 'Type'],
    'slug' => ['tr' => 'tip', 'en' => 'type'],
    'handle' => 'type',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyOption = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Seçenek', 'en' => 'Option'],
    'slug' => ['tr' => 'secenek', 'en' => 'option'],
    'handle' => 'option',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyBody = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Body', 'en' => 'Body'],
    'slug' => ['tr' => 'body', 'en' => 'body'],
    'handle' => 'body',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyMaterial = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Material', 'en' => 'Material'],
    'slug' => ['tr' => 'material', 'en' => 'material'],
    'handle' => 'material',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyTrimMaterial = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Trim Material', 'en' => 'Trim Material'],
    'slug' => ['tr' => 'trim-material', 'en' => 'trim-material'],
    'handle' => 'trim_material',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyTrim = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Trim', 'en' => 'Trim'],
    'slug' => ['tr' => 'trim', 'en' => 'trim'],
    'handle' => 'trim',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyStemMaterial = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Stem Material', 'en' => 'Stem Material'],
    'slug' => ['tr' => 'stem-material', 'en' => 'stem-material'],
    'handle' => 'stem_material',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyEnd = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'End', 'en' => 'End'],
    'slug' => ['tr' => 'end', 'en' => 'end'],
    'handle' => 'end',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyFace = $taxonomyProduct->children()->create([
    'name' => ['tr' => 'Face to face', 'en' => 'Face to face'],
    'slug' => ['tr' => 'face-to-face', 'en' => 'face-to-face'],
    'handle' => 'face_to_face',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$marine = $taxonomyCategory->children()->create([
    'name' => ['tr' => 'Gemi Vanaları', 'en' => 'Marine'],
    'slug' => ['tr' => 'gemi-vanalari', 'en' => 'marine'],
    'handle' => 'marine',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

$waterworks = $taxonomyCategory->children()->create([
    'name' => ['tr' => 'Su Sistemleri Vanaları', 'en' => 'Waterworks'],
    'slug' => ['tr' => 'su-sistemleri-vanalari', 'en' => 'waterworks'],
    'handle' => 'waterworks',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

$maritime = $taxonomyCategory->children()->create([
    'name' => ['tr' => 'Askeri Gemi Vanaları', 'en' => 'Maritime'],
    'slug' => ['tr' => 'askeri-gemi-vanalari', 'en' => 'maritime'],
    'handle' => 'maritime',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

$remote = $taxonomyCategory->children()->create([
    'name' => ['tr' => 'Kontrol Vanaları', 'en' => 'Remote Control'],
    'slug' => ['tr' => 'kontrol-vanalari', 'en' => 'remote-control'],
    'handle' => 'remote-control',
    'isSingleSelect' => 1,
    'isDeletable' => 0,
    'status' => 1,
    'ordernum' => 1
]);

// Variants
$taxonomyVariantsProductCode = $taxonomyVariants->children()->create([
    'name' => ['tr' => 'Ürün Kodu', 'en' => 'Product Code'],
    'slug' => ['tr' => 'urun-kodu', 'en' => 'product-code'],
    'handle' => 'product_code',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyVariantsFlange = $taxonomyVariants->children()->create([
    'name' => ['tr' => 'Flanş', 'en' => 'Flange'],
    'slug' => ['tr' => 'flans', 'en' => 'flange'],
    'handle' => 'flange',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyVariantsWPODFL = $taxonomyVariants->children()->create([
    'name' => ['tr' => 'WP/OD/FL', 'en' => 'WP/OD/FL'],
    'slug' => ['tr' => 'wp-od-fl', 'en' => 'wp-od-fl'],
    'handle' => 'w_p__o_d__f_l',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

$taxonomyVariantsDN = $taxonomyVariants->children()->create([
    'name' => ['tr' => 'DN', 'en' => 'DN'],
    'slug' => ['tr' => 'dn', 'en' => 'dn'],
    'handle' => 'd_n',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1
]);

//Icons
$taxonomyIcons->children()->create([
    'name' => ['tr' => 'Gaz', 'en' => 'Gas'],
    'slug' => ['tr' => 'Gaz', 'en' => 'gas'],
    'handle' => 'gas',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1,
    'image_path' => 'dikkan-gas-icon.webp',
]);

$taxonomyIcons->children()->create([
    'name' => ['tr' => 'Petrol', 'en' => 'Petroleum'],
    'slug' => ['tr' => 'petrol', 'en' => 'petroleum'],
    'handle' => 'petroleum',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1,
    'image_path' => 'dikkan-petroleum-icon.webp',
]);

$taxonomyIcons->children()->create([
    'name' => ['tr' => 'Deniz', 'en' => 'Sea'],
    'slug' => ['tr' => 'deniz', 'en' => 'sea'],
    'handle' => 'sea',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1,
    'image_path' => 'dikkan-sea-icon.webp',
]);

$taxonomyIcons->children()->create([
    'name' => ['tr' => 'Atık su', 'en' => 'Wastewater'],
    'slug' => ['tr' => 'atik-su', 'en' => 'wastewater'],
    'handle' => 'wastewater',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1,
    'image_path' => 'dikkan-wastewater-icon.webp',
]);

$taxonomyIcons->children()->create([
    'name' => ['tr' => 'Su', 'en' => 'Water'],
    'slug' => ['tr' => 'Su', 'en' => 'Water'],
    'handle' => 'water',
    'isSingleSelect' => 1,
    'status' => 1,
    'ordernum' => 1,
    'image_path' => 'dikkan-water-icon.webp',
]);

    }
}
