<?php

namespace Modules\Dummy\Database\Seeders\Modules\OpenAI\versions\v7_5_0;

use App\Models\Permission;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PermissionTableSeeder extends Seeder
{
    /**
     * API v2 user package info (credit limits) — used by permission-api middleware.
     *
     * @return void
     */
    public function run()
    {
        $permissionData = [
            'name' => 'Modules\\OpenAI\\Http\\Controllers\\Api\\v2\\User\\UserController@packageInfo',
            'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Api\\v2\\User\\UserController',
            'controller_name' => 'UserController',
            'method_name' => 'packageInfo',
        ];

        $permission = Permission::updateOrCreate(
            ['name' => $permissionData['name']],
            $permissionData
        );

        DB::table('permission_roles')->upsert(
            [
                'permission_id' => $permission->id,
                'role_id' => 2,
            ],
            ['permission_id', 'role_id'],
            []
        );

        $voicePermission = Permission::updateOrCreate(
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Api\\v2\\User\\VoiceoverController@voices',
            ],
            [
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Api\\v2\\User\\VoiceoverController',
                'controller_name' => 'VoiceoverController',
                'method_name' => 'voices',
            ]
        );

        DB::table('permission_roles')->upsert(
            [
                'permission_id' => $voicePermission->id,
                'role_id' => 2,
            ],
            ['permission_id', 'role_id'],
            []
        );

        $rows = [
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController@index',
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController',
                'controller_name' => 'PlagiarismController',
                'method_name' => 'index',
            ],
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController@delete',
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController',
                'controller_name' => 'PlagiarismController',
                'method_name' => 'delete',
            ],
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController@show',
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController',
                'controller_name' => 'PlagiarismController',
                'method_name' => 'show',
            ],
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController@pdf',
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController',
                'controller_name' => 'PlagiarismController',
                'method_name' => 'pdf',
            ],
            [
                'name' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController@csv',
                'controller_path' => 'Modules\\OpenAI\\Http\\Controllers\\Admin\\PlagiarismController',
                'controller_name' => 'PlagiarismController',
                'method_name' => 'csv',
            ],
        ];

        foreach ($rows as $row) {
            $permission = Permission::updateOrCreate(
                ['name' => $row['name']],
                $row
            );

            DB::table('permission_roles')->upsert(
                [
                    [
                        'permission_id' => $permission->id,
                        'role_id' => 1,
                    ],
                ],
                ['permission_id', 'role_id'],
                []
            );
        }
    }
}
