<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RoleAndPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $role = Role::create(['name' => 'super admin']);
        $permissions = [
            // ========== SETUP & CONFIGURATION ==========
            // Category
            ['parent' => 'category', 'name' => 'category_create'],
            ['parent' => 'category', 'name' => 'category_delete'],
            ['parent' => 'category', 'name' => 'category_edit'],
            ['parent' => 'category', 'name' => 'category_view'],

            // Product
            ['parent' => 'product', 'name' => 'product_create'],
            ['parent' => 'product', 'name' => 'product_delete'],
            ['parent' => 'product', 'name' => 'product_edit'],
            ['parent' => 'product', 'name' => 'product_for_row_material'],
            ['parent' => 'product', 'name' => 'product_for_finish_product'],
            ['parent' => 'product', 'name' => 'product_view'],

            // Product Type
            ['parent' => 'product_type', 'name' => 'product_type_create'],
            ['parent' => 'product_type', 'name' => 'product_type_delete'],
            ['parent' => 'product_type', 'name' => 'product_type_edit'],
            ['parent' => 'product_type', 'name' => 'product_type_view'],

            // Unit
            ['parent' => 'unit', 'name' => 'unit_create'],
            ['parent' => 'unit', 'name' => 'unit_delete'],
            ['parent' => 'unit', 'name' => 'unit_edit'],
            ['parent' => 'unit', 'name' => 'unit_view'],

            // Brand
            ['parent' => 'brand', 'name' => 'brand_create'],
            ['parent' => 'brand', 'name' => 'brand_delete'],
            ['parent' => 'brand', 'name' => 'brand_edit'],
            ['parent' => 'brand', 'name' => 'brand_view'],

            // Color
            ['parent' => 'color', 'name' => 'color_create'],
            ['parent' => 'color', 'name' => 'color_delete'],
            ['parent' => 'color', 'name' => 'color_edit'],
            ['parent' => 'color', 'name' => 'color_view'],

            // Size
            ['parent' => 'size', 'name' => 'size_create'],
            ['parent' => 'size', 'name' => 'size_delete'],
            ['parent' => 'size', 'name' => 'size_edit'],
            ['parent' => 'size', 'name' => 'size_view'],

            // Model
            ['parent' => 'model', 'name' => 'model_create'],
            ['parent' => 'model', 'name' => 'model_delete'],
            ['parent' => 'model', 'name' => 'model_edit'],
            ['parent' => 'model', 'name' => 'model_view'],

            // Origin
            ['parent' => 'origin', 'name' => 'origin_create'],
            ['parent' => 'origin', 'name' => 'origin_delete'],
            ['parent' => 'origin', 'name' => 'origin_edit'],
            ['parent' => 'origin', 'name' => 'origin_view'],

            // Shelf
            ['parent' => 'shelf', 'name' => 'shelf_create'],
            ['parent' => 'shelf', 'name' => 'shelf_delete'],
            ['parent' => 'shelf', 'name' => 'shelf_edit'],
            ['parent' => 'shelf', 'name' => 'shelf_view'],

            // Supplier
            ['parent' => 'supplier', 'name' => 'supplier_create'],
            ['parent' => 'supplier', 'name' => 'supplier_delete'],
            ['parent' => 'supplier', 'name' => 'supplier_edit'],
            ['parent' => 'supplier', 'name' => 'supplier_view'],

            ['parent' => 'cnf', 'name' => 'cnf_create'],
            ['parent' => 'cnf', 'name' => 'cnf_edit'],
            ['parent' => 'cnf', 'name' => 'cnf_delete'],
            ['parent' => 'cnf', 'name' => 'cnf_view'],

            ['parent' => 'shop', 'name' => 'shop_create'],
            ['parent' => 'shop', 'name' => 'shop_delete'],
            ['parent' => 'shop', 'name' => 'shop_edit'],
            ['parent' => 'shop', 'name' => 'shop_view'],

            // Warehouse Setup
            ['parent' => 'warehoue_setup', 'name' => 'opening_super_admin_access'],
            ['parent' => 'warehoue_setup', 'name' => 'warehoue_setup_create'],
            ['parent' => 'warehoue_setup', 'name' => 'warehoue_setup_delete'],
            ['parent' => 'warehoue_setup', 'name' => 'warehoue_setup_edit'],
            ['parent' => 'warehoue_setup', 'name' => 'warehoue_setup_view'],

            // Warehouse
            ['parent' => 'warehoue', 'name' => 'warehoue_view'],

            // Department
            ['parent' => 'department', 'name' => 'department_create'],
            ['parent' => 'department', 'name' => 'department_delete'],
            ['parent' => 'department', 'name' => 'department_edit'],
            ['parent' => 'department', 'name' => 'department_view'],
            ['parent' => 'department', 'name' => 'use_department'],

            // Designation
            ['parent' => 'designation', 'name' => 'designation_create'],
            ['parent' => 'designation', 'name' => 'designation_delete'],
            ['parent' => 'designation', 'name' => 'designation_edit'],
            ['parent' => 'designation', 'name' => 'designation_view'],

            // Client Contact Person
            ['parent' => 'client_contact_person', 'name' => 'client_contact_person_create'],
            ['parent' => 'client_contact_person', 'name' => 'client_contact_person_delete'],
            ['parent' => 'client_contact_person', 'name' => 'client_contact_person_edit'],
            ['parent' => 'client_contact_person', 'name' => 'client_contact_person_view'],

            // Branch
            ['parent' => 'branch', 'name' => 'branch_create'],
            ['parent' => 'branch', 'name' => 'branch_delete'],
            ['parent' => 'branch', 'name' => 'branch_edit'],
            ['parent' => 'branch', 'name' => 'branch_view'],
            ['parent' => 'branch', 'name' => 'use_branch'],

            // Expanse Category
            ['parent' => 'expanse_category', 'name' => 'expanse_category_create'],
            ['parent' => 'expanse_category', 'name' => 'expanse_category_delete'],
            ['parent' => 'expanse_category', 'name' => 'expanse_category_edit'],
            ['parent' => 'expanse_category', 'name' => 'expanse_category_view'],

            // Quotation Type
            ['parent' => 'quotation_type', 'name' => 'quotation_type_create'],
            ['parent' => 'quotation_type', 'name' => 'quotation_type_delete'],
            ['parent' => 'quotation_type', 'name' => 'quotation_type_edit'],
            ['parent' => 'quotation_type', 'name' => 'quotation_type_view'],

            // Table Setup
            ['parent' => 'table_setup', 'name' => 'table_setup_create'],
            ['parent' => 'table_setup', 'name' => 'table_setup_delete'],
            ['parent' => 'table_setup', 'name' => 'table_setup_edit'],
            ['parent' => 'table_setup', 'name' => 'table_setup_view'],

            // ========== OPERATIONS ==========
            // Adjustment
            ['parent' => 'adjustment', 'name' => 'adjustment_create'],
            ['parent' => 'adjustment', 'name' => 'adjustment_delete'],
            ['parent' => 'adjustment', 'name' => 'adjustment_edit'],
            ['parent' => 'adjustment', 'name' => 'adjustment_super_admin_access'],
            ['parent' => 'adjustment', 'name' => 'adjustment_view'],

            // Barcode
            ['parent' => 'barcode', 'name' => 'barcode_view'],

            // Checker Maker
            ['parent' => 'checker_maker', 'name' => 'checker_maker_view'],

            // Purchase Maker
            ['parent' => 'purchase_maker', 'name' => 'cartoon_auto_calculation'],
            ['parent' => 'purchase_maker', 'name' => 'purchase_maker_create'],
            ['parent' => 'purchase_maker', 'name' => 'purchase_maker_delete'],
            ['parent' => 'purchase_maker', 'name' => 'purchase_maker_edit'],
            ['parent' => 'purchase_maker', 'name' => 'purchase_maker_prient'],
            ['parent' => 'purchase_maker', 'name' => 'purchase_maker_view'],

            // Purchase
            ['parent' => 'purchase', 'name' => 'purchase_create'],
            ['parent' => 'purchase', 'name' => 'purchase_delete'],
            ['parent' => 'purchase', 'name' => 'purchase_direct_return'],
            ['parent' => 'purchase', 'name' => 'purchase_edit'],
            ['parent' => 'purchase', 'name' => 'purchase_payment_view'],
            ['parent' => 'purchase', 'name' => 'purchase_prient'],
            ['parent' => 'purchase', 'name' => 'purchase_return_super_admin_access'],
            ['parent' => 'purchase', 'name' => 'purchase_return_view'],
            ['parent' => 'purchase', 'name' => 'purchase_super_admin_access'],
            ['parent' => 'purchase', 'name' => 'purchase_view'],
            ['parent' => 'purchase', 'name' => 'purchase_conversion_batch'],
            ['parent' => 'purchase', 'name' => 'supplier_wise_product_view'],

            // Requsition
            ['parent' => 'requsition', 'name' => 'requsition_create'],
            ['parent' => 'requsition', 'name' => 'requsition_delete'],
            ['parent' => 'requsition', 'name' => 'requsition_edit'],
            ['parent' => 'requsition', 'name' => 'requsition_prient'],
            ['parent' => 'requsition', 'name' => 'requsition_product_alert_print'],
            ['parent' => 'requsition', 'name' => 'requsition_stock_view'],
            ['parent' => 'requsition', 'name' => 'requsition_document_wise'],
            ['parent' => 'requsition', 'name' => 'requsition_view'],

            // Product Shipping
            ['parent' => 'product_shiping', 'name' => 'product_shiping_edit'],
            ['parent' => 'product_shiping', 'name' => 'product_shiping_prient'],
            ['parent' => 'product_shiping', 'name' => 'product_shiping_view'],

            // Product Received
            ['parent' => 'product_received', 'name' => 'product_received_view'],

            // Used Product
            ['parent' => 'used_product', 'name' => 'used_product_view'],

            // Transfer
            ['parent' => 'transfer', 'name' => 'transfer_super_admin_access'],
            ['parent' => 'transfer', 'name' => 'transfer_view'],

            // POS
            ['parent' => 'pos', 'name' => 'fqty_pos'],
            ['parent' => 'pos', 'name' => 'no_due_pos'],
            ['parent' => 'pos', 'name' => 'no_qty_validation_check'],
            ['parent' => 'pos', 'name' => 'pos_invoice_view'],
            ['parent' => 'pos', 'name' => 'pos_qty_view'],
            ['parent' => 'pos', 'name' => 'pos_view'],
            ['parent' => 'pos', 'name' => 'pos_whatsapp_notification'],
            ['parent' => 'pos', 'name' => 'pos_with_multiple_payment'],
            ['parent' => 'pos', 'name' => 'pos_restricted_mode'],
            ['parent' => 'pos', 'name' => 'table_multiple_customer_use'],
            ['parent' => 'pos', 'name' => 'enforce_customer_selection'],
            // Sales
            ['parent' => 'sales', 'name' => 'sales_admin_access'],
            ['parent' => 'sales', 'name' => 'sales_create'],
            ['parent' => 'sales', 'name' => 'sales_delete'],
            ['parent' => 'sales', 'name' => 'sales_direct_return'],
            ['parent' => 'sales', 'name' => 'sales_edit'],
            ['parent' => 'sales', 'name' => 'sales_invoice_due_view'],
            ['parent' => 'sales', 'name' => 'sales_order_view'],
            ['parent' => 'sales', 'name' => 'sales_person_pos_view'],
            ['parent' => 'sales', 'name' => 'sales_return'],
            ['parent' => 'sales', 'name' => 'sales_return_super_admin_access'],
            ['parent' => 'sales', 'name' => 'sales_vat_view'],
            ['parent' => 'sales', 'name' => 'sales_view'],
            ['parent' => 'sales', 'name' => 'sales_return_apply_on_cash_report'],

            // Sales Person
            ['parent' => 'sales_person', 'name' => 'sales_person_create'],
            ['parent' => 'sales_person', 'name' => 'sales_person_delete'],
            ['parent' => 'sales_person', 'name' => 'sales_person_edit'],
            ['parent' => 'sales_person', 'name' => 'sales_person_view'],
            ['parent' => 'sales_person', 'name' => 'sales_person_view_in_pos'],

            // Customer Retailer
            ['parent' => 'customer_retailer', 'name' => 'customer_retailer_create'],
            ['parent' => 'customer_retailer', 'name' => 'customer_retailer_delete'],
            ['parent' => 'customer_retailer', 'name' => 'customer_retailer_edit'],
            ['parent' => 'customer_retailer', 'name' => 'customer_retailer_view'],

            // Customer Branch
            ['parent' => 'customer_branch', 'name' => 'customer_branch_create'],
            ['parent' => 'customer_branch', 'name' => 'customer_branch_delete'],
            ['parent' => 'customer_branch', 'name' => 'customer_branch_edit'],
            ['parent' => 'customer_branch', 'name' => 'customer_branch_view'],
            ['parent' => 'customer_branch', 'name' => 'use_customer_branch'],

            // Customer Retailer Opening
            ['parent' => 'customer_retailer_opening', 'name' => 'customer_retailer_opening_create'],
            ['parent' => 'customer_retailer_opening', 'name' => 'customer_retailer_opening_delete'],
            ['parent' => 'customer_retailer_opening', 'name' => 'customer_retailer_opening_edit'],
            ['parent' => 'customer_retailer_opening', 'name' => 'customer_retailer_opening_view'],
            ['parent' => 'customer_retailer_opening', 'name' => 'customer_retailer_superAdmin_access'],

            // Customer Collection
            ['parent' => 'customer_collection', 'name' => 'customer_collection_create'],
            ['parent' => 'customer_collection', 'name' => 'customer_collection_delete'],
            ['parent' => 'customer_collection', 'name' => 'customer_collection_edit'],
            ['parent' => 'customer_collection', 'name' => 'customer_collection_superAdmin_access'],
            ['parent' => 'customer_collection', 'name' => 'customer_collection_view'],
            ['parent' => 'customer_collection', 'name' => 'customer_retailer_sales_person_view'],

            // Expanse
            ['parent' => 'expanse', 'name' => 'expanse_create'],
            ['parent' => 'expanse', 'name' => 'expanse_delete'],
            ['parent' => 'expanse', 'name' => 'expanse_edit'],
            ['parent' => 'expanse', 'name' => 'expanse_view'],

            // Damage
            ['parent' => 'damage', 'name' => 'damage_create'],
            ['parent' => 'damage', 'name' => 'damage_delete'],
            ['parent' => 'damage', 'name' => 'damage_edit'],
            ['parent' => 'damage', 'name' => 'damage_view'],
            ['parent' => 'damage', 'name' => 'sales_person_show_in_damage'],

            // Work Order
            ['parent' => 'work_order', 'name' => 'work_order_create'],
            ['parent' => 'work_order', 'name' => 'work_order_delete'],
            ['parent' => 'work_order', 'name' => 'work_order_edit'],
            ['parent' => 'work_order', 'name' => 'work_order_print'],
            ['parent' => 'work_order', 'name' => 'work_order_view'],

            // ========== LOAN & FINANCE ==========
            // Loan Provider
            ['parent' => 'loan_provider', 'name' => 'loan_provider_create'],
            ['parent' => 'loan_provider', 'name' => 'loan_provider_delete'],
            ['parent' => 'loan_provider', 'name' => 'loan_provider_edit'],
            ['parent' => 'loan_provider', 'name' => 'loan_provider_view'],

            // Loan Type
            ['parent' => 'loan_type', 'name' => 'loan_type_create'],
            ['parent' => 'loan_type', 'name' => 'loan_type_delete'],
            ['parent' => 'loan_type', 'name' => 'loan_type_edit'],
            ['parent' => 'loan_type', 'name' => 'loan_type_view'],

            // Loan Entry
            ['parent' => 'loan_entry', 'name' => 'loan_entry_create'],
            ['parent' => 'loan_entry', 'name' => 'loan_entry_delete'],
            ['parent' => 'loan_entry', 'name' => 'loan_entry_edit'],
            ['parent' => 'loan_entry', 'name' => 'loan_entry_view'],

            // LC Module
            ['parent' => 'lc_module', 'name' => 'lc_module_create'],
            ['parent' => 'lc_module', 'name' => 'lc_module_delete'],
            ['parent' => 'lc_module', 'name' => 'lc_module_edit'],
            ['parent' => 'lc_module', 'name' => 'lc_module_view'],

            // ========== EMPLOYEE MANAGEMENT ==========
            // Employee Setup
            ['parent' => 'employee_setup', 'name' => 'employee_setup_create'],
            ['parent' => 'employee_setup', 'name' => 'employee_setup_delete'],
            ['parent' => 'employee_setup', 'name' => 'employee_setup_edit'],
            ['parent' => 'employee_setup', 'name' => 'employee_setup_view'],

            // Employee Salary
            ['parent' => 'employee_salary', 'name' => 'employee_salary_create'],
            ['parent' => 'employee_salary', 'name' => 'employee_salary_delete'],
            ['parent' => 'employee_salary', 'name' => 'employee_salary_edit'],
            ['parent' => 'employee_salary', 'name' => 'employee_salary_view'],

            // ========== REPORTS ==========
            // Report
            ['parent' => 'report', 'name' => 'client_retailer_due_details'],
            ['parent' => 'report', 'name' => 'client_retailer_due_summary'],
            ['parent' => 'report', 'name' => 'client_retailer_opening'],
            ['parent' => 'report', 'name' => 'client_retailer_payment_details'],
            ['parent' => 'report', 'name' => 'company_due_summary'],
            ['parent' => 'report', 'name' => 'company_opening'],
            ['parent' => 'report', 'name' => 'current_stock_position'],
            ['parent' => 'report', 'name' => 'current_value_show'],
            ['parent' => 'report', 'name' => 'customer_ledger_report'],
            ['parent' => 'report', 'name' => 'daily_khata'],
            ['parent' => 'report', 'name' => 'damage_report'],
            ['parent' => 'report', 'name' => 'discount_report'],
            ['parent' => 'report', 'name' => 'expense'],
            ['parent' => 'report', 'name' => 'expiredReportView'],
            ['parent' => 'report', 'name' => 'income_statement'],
            ['parent' => 'report', 'name' => 'opening_stock'],
            ['parent' => 'report', 'name' => 'product_report'],
            ['parent' => 'report', 'name' => 'purchase_details'],
            ['parent' => 'report', 'name' => 'purchase_return'],
            ['parent' => 'report', 'name' => 'purchase_summary'],
            ['parent' => 'report', 'name' => 'report_super_admin_access'],
            ['parent' => 'report', 'name' => 'salse_details'],
            ['parent' => 'report', 'name' => 'salse_report'],
            ['parent' => 'report', 'name' => 'sales_details'],
            ['parent' => 'report', 'name' => 'sales_person_salary_view'],
            ['parent' => 'report', 'name' => 'sales_return_report'],
            ['parent' => 'report', 'name' => 'sales_summary'],
            ['parent' => 'report', 'name' => 'stock_report'],
            ['parent' => 'report', 'name' => 'supplier_ledger_report'],
            ['parent' => 'report', 'name' => 'warehouse_transfer_report'],

            // ========== SYSTEM & SETTINGS ==========
            // Deshbord
            ['parent' => 'deshbord', 'name' => 'deshbord_view'],
            ['parent' => 'deshbord', 'name' => 'deshbord_profite_view'],
            ['parent' => 'deshbord', 'name' => 'deshbord_net_income_view'],

            // Role
            ['parent' => 'role', 'name' => 'role_view'],

            // Setting
            ['parent' => 'setting', 'name' => 'setting_view'],

            // Belongs to Business
            ['parent' => 'belongs_to_business', 'name' => 'belongs_to_business'],
        ];

        foreach ($permissions as $key => $value) {
            Permission::create($value);
        }
        $role->syncPermissions(Permission::whereNotIn('name', ['belongs_to_business', 'shop_warehouse_view', 'requsition_create', 'requsition_edit', 'requsition_delete', 'requsition_view', 'requsition_prient', 'used_product_view', 'product_received_view'])->get());

        User::first()->assignRole($role);
    }
}
