<?php

namespace Modules\Approval\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Collection;
use Modules\Approval\Models\ApprovalWorkflow;
use Modules\Approval\Models\ApprovalWorkflowStep;
use Modules\Auth\Repositories\RoleRepository;
use Modules\Core\Enums\RolesEnum;
use Modules\Core\Enums\StatusEnum;

class ApprovalWorkflowSeeder extends Seeder
{
    public function __construct(
        protected RoleRepository $roleRepository
    ) {}
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $roles = $this->roleRepository->all(
            includeSuperAdmin: true,
            columns: ['id', 'name']
        )->keyBy('name');

        $this->purchaseRequisitionWorkflow($roles);
        $this->purchaseInvoiceWorkflow($roles);
        $this->purchasePaymentWorkflow($roles);
    }

    private function purchaseRequisitionWorkflow(Collection $roles): void
    {
        $adminRoleId = $roles->get(RolesEnum::ADMIN->value)?->id;
        $managerRoleId = $roles->get(RolesEnum::MANAGER->value)?->id;
        $edRoleId = $roles->get(RolesEnum::ED->value)?->id;

        $workflow = ApprovalWorkflow::query()->updateOrCreate(
            [
                'code' => ApprovalWorkflow::PURCHASE_REQUISITION,
            ],
            [
                'name' => 'Purchase Requisition',
                'status' => StatusEnum::ACTIVE,
            ]
        );

        $steps = [
            [
                'level' => 1,
                'name' => 'Admin Approval',
                'permission_name' => 'inventory.purchase.requisition-approve',
                'role_id' => $adminRoleId,
            ],
            [
                'level' => 2,
                'name' => 'Manager Approval',
                'permission_name' => 'inventory.purchase.requisition-approve',
                'role_id' => $managerRoleId,
            ],
            [
                'level' => 3,
                'name' => 'Executive Director Approval',
                'permission_name' => 'inventory.purchase.requisition-approve',
                'role_id' => $edRoleId,
            ]
        ];

        foreach ($steps as $step) {
            ApprovalWorkflowStep::query()->updateOrCreate(
                [
                    'approval_workflow_id' => $workflow->id,
                    'level' => $step['level'],
                ],
                [
                    'role_id' => $step['role_id'],
                    'name' => $step['name'],
                    'permission_name' => $step['permission_name'],
                    'is_required' => true,
                ]
            );
        }
    }

    private function purchaseInvoiceWorkflow(Collection $roles): void
    {
        $adminRoleId = $roles->get(RolesEnum::ADMIN->value)?->id;

        $workflow = ApprovalWorkflow::query()->updateOrCreate(
            [
                'code' => ApprovalWorkflow::PURCHASE_INVOICE,
            ],
            [
                'name' => 'Purchase Invoice',
                'status' => StatusEnum::ACTIVE,
            ]
        );

        $steps = [
            [
                'level' => 1,
                'name' => 'Admin Approval',
                'permission_name' => 'inventory.purchase.approve',
                'role_id' => $adminRoleId,
            ]
        ];

        foreach ($steps as $step) {
            ApprovalWorkflowStep::query()->updateOrCreate(
                [
                    'approval_workflow_id' => $workflow->id,
                    'level' => $step['level'],
                ],
                [
                    'role_id' => $step['role_id'],
                    'name' => $step['name'],
                    'permission_name' => $step['permission_name'],
                    'is_required' => true,
                ]
            );
        }
    }

    private function purchasePaymentWorkflow(Collection $roles): void
    {
        $adminRoleId = $roles->get(RolesEnum::ADMIN->value)?->id;
        $managerRoleId = $roles->get(RolesEnum::MANAGER->value)?->id;
        $edRoleId = $roles->get(RolesEnum::ED->value)?->id;

        $workflow = ApprovalWorkflow::query()->updateOrCreate(
            [
                'code' => ApprovalWorkflow::PURCHASE_PAYMENT,
            ],
            [
                'name' => 'Purchase Payment',
                'status' => StatusEnum::ACTIVE,
            ]
        );

        $steps = [
            [
                'level' => 1,
                'name' => 'Admin Approval',
                'permission_name' => 'inventory.purchase.payment-approve',
                'role_id' => $adminRoleId,
            ],
            [
                'level' => 2,
                'name' => 'Manager Approval',
                'permission_name' => 'inventory.purchase.payment-approve',
                'role_id' => $managerRoleId,
            ],
            [
                'level' => 3,
                'name' => 'Executive Director Approval',
                'permission_name' => 'inventory.purchase.payment-approve',
                'role_id' => $edRoleId,
            ]
        ];

        foreach ($steps as $step) {
            ApprovalWorkflowStep::query()->updateOrCreate(
                [
                    'approval_workflow_id' => $workflow->id,
                    'level' => $step['level'],
                ],
                [
                    'role_id' => $step['role_id'],
                    'name' => $step['name'],
                    'permission_name' => $step['permission_name'],
                    'is_required' => true,
                ]
            );
        }
    }
}
