<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Action;
use App\Models\Package;
use App\Models\Page;
use App\Models\Profile;
use App\Models\ProfilePagePackage;
use App\Models\Status;

class UsuarioAdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        try {
            DB::beginTransaction();

            $newUser = new User;
            $newUser->name = 'Administrador';
            $newUser->email = 'admin@fides.com';
            $newUser->password = Hash::make('Caracas.2021*');
            $newUser->save();

            $arrayAcciones = ['Ver', 'Crear', 'Editar', 'Borrar'];

            foreach ($arrayAcciones as $value) {
                $newAction = new Action;
                $newAction->id_user = $newUser->id;
                $newAction->id_user_update = $newUser->id;
                $newAction->name = $value;
                $newAction->save();
            }

            $newPackage = new Package;
            $newPackage->id_user = $newUser->id;
            $newPackage->id_user_update = $newUser->id;
            $newPackage->name = 'Ver';
            $newPackage->save();

            $actions = [1];
            $newPackage->actions()->attach($actions);

            $newPackage = new Package;
            $newPackage->id_user = $newUser->id;
            $newPackage->id_user_update = $newUser->id;
            $newPackage->name = 'Ver, Crear';
            $newPackage->save();

            $actions = [1, 2];
            $newPackage->actions()->attach($actions);

            $newPackage = new Package;
            $newPackage->id_user = $newUser->id;
            $newPackage->id_user_update = $newUser->id;
            $newPackage->name = 'Ver, Crear, Editar';
            $newPackage->save();

            $actions = [1, 2, 3];
            $newPackage->actions()->attach($actions);

            $newPackage = new Package;
            $newPackage->id_user = $newUser->id;
            $newPackage->id_user_update = $newUser->id;
            $newPackage->name = 'Ver, Crear, Editar, Borrar';
            $newPackage->save();

            $actions = [1, 2, 3, 4];
            $newPackage->actions()->attach($actions);

            $arrayPages = [
                [
                    'ruta' => null,
                    'nombreruta' => null,
                    'nombre' => 'Operaciones',
                    'icon' => 'fas,sitemap',
                    'packages' => [1],
                    'padre' => true,
                    'childrens' => [
                        [
                            'ruta' => '/payment_link',
                            'nombreruta' => 'payment_link',
                            'nombre' => 'Link de Pago',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                    ]
                ],

                [
                    'ruta' => null,
                    'nombreruta' => null,
                    'nombre' => 'Configuración',
                    'icon' => 'fas,gears',
                    'packages' => [1],
                    'padre' => true,
                    'childrens' => [
                        [
                            'ruta' => '/users',
                            'nombreruta' => 'users',
                            'nombre' => 'Usuarios',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                        [
                            'ruta' => '/pages',
                            'nombreruta' => 'pages',
                            'nombre' => 'Páginas',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                        [
                            'ruta' => '/actions',
                            'nombreruta' => 'actions',
                            'nombre' => 'Acciones',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                        [
                            'ruta' => '/packages',
                            'nombreruta' => 'packages',
                            'nombre' => 'Paquetes',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                        [
                            'ruta' => '/profiles',
                            'nombreruta' => 'profiles',
                            'nombre' => 'Perfiles',
                            'icon' => null,
                            'packages' => [1, 2, 3, 4]
                        ],
                    ]
                ],
            ];

            $newProfile = new Profile;
            $newProfile->id_user = $newUser->id;
            $newProfile->id_user_update = $newUser->id;
            $newProfile->name = 'Administrador';
            $newProfile->description = 'Administrador';
            $newProfile->save();

            foreach ($arrayPages as $value) {
                $newPage = new Page;
                $newPage->id_user = $newUser->id;
                $newPage->id_user_update = $newUser->id;
                $newPage->name = $value['nombre'];
                $newPage->icon = $value['icon'];
                $newPage->route = $value['ruta'];
                $newPage->name_route = $value['nombreruta'];
                $newPage->save();

                $newPage->packages()->attach($value['packages']);

                $newProfilePagePackage = new ProfilePagePackage;
                $newProfilePagePackage->id_profile = $newProfile->id;
                $newProfilePagePackage->id_page = $newPage->id;
                $newProfilePagePackage->id_package = 1;
                $newProfilePagePackage->save();

                foreach ($value['childrens'] as $value2) {
                    $newPageChildren = new Page;
                    $newPageChildren->id_user = $newUser->id;
                    $newPageChildren->id_user_update = $newUser->id;
                    $newPageChildren->id_page_parent = $newPage->id;
                    $newPageChildren->name = $value2['nombre'];
                    $newPageChildren->route = isset($value['route']) ? ('/' . $value['route'] . $value2['ruta']) : $value2['ruta'];
                    $newPageChildren->name_route = isset($value['route']) ? ($value['route'] . $value2['nombreruta']) : $value2['nombreruta'];
                    $newPageChildren->save();
                    $newPageChildren->packages()->attach($value2['packages']);

                    $newProfilePagePackage = new ProfilePagePackage;
                    $newProfilePagePackage->id_profile = $newProfile->id;
                    $newProfilePagePackage->id_page = $newPageChildren->id;
                    $newProfilePagePackage->id_package = 4;
                    $newProfilePagePackage->save();
                }
            }

            $updateUser = User::find($newUser->id);
            $updateUser->id_profile = $newProfile->id;
            $updateUser->save();

            $statuses = ['ENVIADO AL CLIENTE', 'PAGO REALIZADO CON ÉXITO', 'SIN RESPUESTA DEL CLIENTE', 'LINK CANCELADO', 'TRANSACCIÓN FALLIDA'];
            foreach ($statuses as $value) {
                $newStatus = new Status;
                $newStatus->id_user = $newUser->id;
                $newStatus->id_user_update = $newUser->id;
                $newStatus->name = $value;
                $newStatus->save();
            }

            DB::commit();
        } catch (Exception $th) {
            DB::rollBack();
            dd($th);
        }
    }
}
