<?php

namespace Database\Seeders;

use App\Enums\UserTypeEnum;
use App\Models\Permission;
use App\Models\UserTypePermission;
use Illuminate\Database\Seeder;

class UserTypePermissionSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        foreach (UserTypeEnum::cases() as $userType) {
            switch ($userType) {
                case UserTypeEnum::ADMIN:
                    $permissions = Permission::get()->map(function ($permission) {
                        return [
                            'scope' => $permission->scope,
                            'bits'  => $permission->bits,
                        ];
                    })->toArray();

                    $this->seedUserTypePermissions($permissions, UserTypeEnum::ADMIN->value);

                    break;

                case UserTypeEnum::USER:
                    $userPermissions = [
                        ['scope' => 'config.address',                  'bits' => 271],
                        ['scope' => 'config.city',                     'bits' => 1],
                        ['scope' => 'config.client',                   'bits' => 271],
                        ['scope' => 'config.client_favorite_provider', 'bits' => 271],
                        ['scope' => 'config.client_payment_method',    'bits' => 271],
                        ['scope' => 'config.client_provider_block',    'bits' => 271],
                        ['scope' => 'config.country',                  'bits' => 1],
                        ['scope' => 'config.custom_schedule',          'bits' => 271],
                        ['scope' => 'config.improvement_type',         'bits' => 271],
                        ['scope' => 'config.media',                    'bits' => 271],
                        ['scope' => 'config.provider',                 'bits' => 271],
                        ['scope' => 'config.provider_blocked_day',     'bits' => 271],
                        ['scope' => 'config.provider_client_block',    'bits' => 271],
                        ['scope' => 'config.provider_services_types',  'bits' => 271],
                        ['scope' => 'config.provider_speciality',      'bits' => 271],
                        ['scope' => 'config.provider_working_day',     'bits' => 271],
                        ['scope' => 'config.review',                   'bits' => 271],
                        ['scope' => 'config.review_improvement',       'bits' => 271],
                        ['scope' => 'config.schedule',                 'bits' => 271],
                        ['scope' => 'config.service_type',             'bits' => 271],
                        ['scope' => 'config.speciality',               'bits' => 271],
                        ['scope' => 'config.state',                    'bits' => 1],
                        ['scope' => 'config.user',                     'bits' => 5],
                        ['scope' => 'dashboard',                       'bits' => 1],
                        ['scope' => 'notification',                    'bits' => 271],
                        ['scope' => 'payment',                         'bits' => 257],
                        ['scope' => 'payment-split',                   'bits' => 1],
                        ['scope' => 'support.request',                 'bits' => 271],
                    ];

                    $this->seedUserTypePermissions($userPermissions, UserTypeEnum::USER->value);

                    break;

                case UserTypeEnum::PROVIDER:
                    $providerPermissions = [
                        ['scope' => 'config.address',                 'bits' => 271],
                        ['scope' => 'config.city',                    'bits' => 1],
                        ['scope' => 'config.custom_schedule',         'bits' => 271],
                        ['scope' => 'config.improvement_type',        'bits' => 1],
                        ['scope' => 'config.provider',                'bits' => 5],
                        ['scope' => 'config.provider_blocked_day',    'bits' => 271],
                        ['scope' => 'config.provider_client_block',   'bits' => 9],
                        ['scope' => 'config.provider_services_types', 'bits' => 271],
                        ['scope' => 'config.provider_working_day',    'bits' => 271],
                        ['scope' => 'config.review',                  'bits' => 271],
                        ['scope' => 'config.schedule',                'bits' => 271],
                        ['scope' => 'config.service_type',            'bits' => 1],
                        ['scope' => 'config.user',                    'bits' => 5],
                        ['scope' => 'dashboard',                      'bits' => 1],
                        ['scope' => 'notification',                   'bits' => 271],
                    ];

                    $this->seedUserTypePermissions($providerPermissions, UserTypeEnum::PROVIDER->value);

                    break;

                case UserTypeEnum::CLIENT:
                    $clientPermissions = [
                        ['scope' => 'config.address',                  'bits' => 271],
                        ['scope' => 'config.city',                     'bits' => 1],
                        ['scope' => 'config.client_favorite_provider', 'bits' => 271],
                        ['scope' => 'config.client_payment_method',    'bits' => 271],
                        ['scope' => 'config.client_provider_block',    'bits' => 9],
                        ['scope' => 'config.custom_schedule',          'bits' => 271],
                        ['scope' => 'config.improvement_type',         'bits' => 1],
                        ['scope' => 'config.provider_blocked_day',     'bits' => 271],
                        ['scope' => 'config.provider_working_day',     'bits' => 271],
                        ['scope' => 'config.review',                   'bits' => 271],
                        ['scope' => 'config.schedule',                 'bits' => 271],
                        ['scope' => 'config.service_type',             'bits' => 271],
                        ['scope' => 'config.speciality',               'bits' => 271],
                        ['scope' => 'config.user',                     'bits' => 5],
                        ['scope' => 'dashboard',                       'bits' => 1],
                        ['scope' => 'notification',                    'bits' => 271],
                    ];

                    $this->seedUserTypePermissions($clientPermissions, UserTypeEnum::CLIENT->value);

                    break;
            }
        }
    }

    private function seedUserTypePermissions(array $permissions, string $userType): void
    {
        foreach ($permissions as $permissionData) {
            $permission = Permission::where('scope', $permissionData['scope'])->first();

            if ($permission) {
                UserTypePermission::updateOrCreate(
                    [
                        'user_type'     => $userType,
                        'permission_id' => $permission->id,
                    ],
                    [
                        'bits' => $permissionData['bits'],
                    ]
                );
            }
        }
    }
}
