<?php

namespace App\Enums;

use App\Traits\ProvidesEnumOptions;

enum Role: string
{
    use ProvidesEnumOptions;

    case DEVELOPER = 'developer';
    case OWNER = 'owner';
    case ADMIN_TOKO = 'admin-toko';
    case ADMIN_BAHAN_BAKU = 'admin-bahan-baku';
    case DIREKTUR = 'direktur';
    case MARKETING_OFFLINE = 'marketing-offline';
    case MARKETING_ONLINE = 'marketing-online';
    case CASHIER = 'cashier';
    case NON_OPERATOR = 'non-operator';
    case STOK_OPNAME = 'stok-opname';

    public function label(): string
    {
        return match ($this) {
            self::DEVELOPER => 'Developer',
            self::OWNER => 'Owner',
            self::ADMIN_TOKO => 'Admin Toko',
            self::ADMIN_BAHAN_BAKU => 'Admin Bahan Baku',
            self::DIREKTUR => 'Direktur',
            self::MARKETING_OFFLINE => 'Marketing Offline',
            self::MARKETING_ONLINE => 'Marketing Online',
            self::CASHIER => 'Kasir',
            self::NON_OPERATOR => 'Non Operator',
            self::STOK_OPNAME => 'Stok Opname',
        };
    }

    public function permissions(): array
    {
        return match ($this) {
            self::DEVELOPER, self::OWNER => array_values(array_filter(
                Permission::cases(),
                fn (Permission $permission) => ! in_array($permission, [
                    Permission::ATTENDANCES_CREATE,
                    Permission::ATTENDANCES_DELETE,

                    Permission::ROLES_VIEW,
                    Permission::ROLES_CREATE,
                    Permission::ROLES_UPDATE,
                    Permission::ROLES_DELETE,

                    Permission::ACTIVITY_LOGS_VIEW,
                ], true)
            )),

            self::DIREKTUR => [
                Permission::DASHBOARD_VIEW,
                Permission::DASHBOARD_ATTENDANCE,
                Permission::DASHBOARD_CASH,
                Permission::DASHBOARD_REVENUE,
                Permission::DASHBOARD_EXPENSE,
                Permission::DASHBOARD_ORDERS_CHANNEL,
                Permission::DASHBOARD_ORDERS_PAYMENT,
                Permission::DASHBOARD_ORDERS_MARKETING,
                Permission::DASHBOARD_ORDERS_STATUS,
                Permission::ANALYSIS_VIEW,

                Permission::ANALYSIS_ATTENDANCE,
                Permission::ANALYSIS_CASH,
                Permission::ANALYSIS_PRODUCT_STOCK,
                Permission::ANALYSIS_REVENUE,
                Permission::ANALYSIS_EXPENSE,
                Permission::ANALYSIS_BUSY_HOURS,
                Permission::ANALYSIS_PROFIT_ORDERS,
                Permission::ANALYSIS_PROFIT_HPP,
                Permission::ANALYSIS_PROFIT_GROSS,
                Permission::ANALYSIS_PROFIT_MARGIN,
                Permission::ANALYSIS_TOP_CUSTOMERS,
                Permission::ANALYSIS_TOP_PRODUCTS,

                Permission::EMPLOYEES_VIEW,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,
                Permission::ATTENDANCES_DELETE,
                Permission::ATTENDANCES_MANAGE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::CATEGORIES_VIEW,

                Permission::CUSTOMERS_VIEW,

                Permission::PRODUCTS_VIEW,

                Permission::ORDERS_VIEW,

                Permission::CUTTINGS_VIEW,

                Permission::CASH_VIEW,

                Permission::EXPENSES_VIEW,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
            ],

            self::ADMIN_TOKO => [
                Permission::DASHBOARD_VIEW,

                Permission::DASHBOARD_CASH,
                Permission::DASHBOARD_REVENUE,
                Permission::DASHBOARD_EXPENSE,
                Permission::DASHBOARD_ORDERS_CHANNEL,
                Permission::DASHBOARD_ORDERS_PAYMENT,
                Permission::DASHBOARD_ORDERS_MARKETING,
                Permission::DASHBOARD_ORDERS_STATUS,
                Permission::ANALYSIS_VIEW,

                Permission::ANALYSIS_ATTENDANCE,
                Permission::ANALYSIS_CASH,
                Permission::ANALYSIS_PRODUCT_STOCK,
                Permission::ANALYSIS_REVENUE,
                Permission::ANALYSIS_EXPENSE,
                Permission::ANALYSIS_BUSY_HOURS,
                Permission::ANALYSIS_TOP_SUPPLIERS,
                Permission::ANALYSIS_TOP_CUSTOMERS,
                Permission::ANALYSIS_TOP_PRODUCTS,

                Permission::STOCKS_VIEW,

                Permission::STOK_OPNAMES_VIEW,
                Permission::STOK_OPNAMES_VERIFY,

                Permission::CUTTINGS_VERIFY,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,
                Permission::ATTENDANCES_DELETE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::CATEGORIES_VIEW,
                Permission::CATEGORIES_CREATE,
                Permission::CATEGORIES_UPDATE,
                Permission::CATEGORIES_DELETE,

                Permission::CUSTOMERS_VIEW,
                Permission::CUSTOMERS_CREATE,
                Permission::CUSTOMERS_UPDATE,
                Permission::CUSTOMERS_DELETE,

                Permission::PRODUCTS_VIEW,

                Permission::OWNER_VERIFICATIONS_VIEW,

                Permission::ORDERS_VIEW,
                Permission::ORDERS_CREATE,
                Permission::ORDERS_UPDATE,
                Permission::ORDERS_DELETE,
                Permission::ORDERS_SEND,
                Permission::ORDERS_COMPLETE,
                Permission::ORDERS_CANCEL,

                Permission::CASH_VIEW,
                Permission::CASH_DEPOSIT,
                Permission::CASH_WITHDRAW,
                Permission::CASH_UPDATE,
                Permission::CASH_DELETE,

                Permission::EXPENSES_VIEW,
                Permission::EXPENSES_CREATE,
                Permission::EXPENSES_UPDATE,
                Permission::EXPENSES_DELETE,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
                Permission::PAYROLL_ADJUST,

                Permission::SETTINGS_VIEW,
                Permission::SETTINGS_UPDATE,
            ],

            self::CASHIER => [
                Permission::DASHBOARD_VIEW,
                Permission::DASHBOARD_ATTENDANCE,
                Permission::DASHBOARD_REVENUE,
                Permission::DASHBOARD_ORDERS_CHANNEL,
                Permission::DASHBOARD_ORDERS_PAYMENT,
                Permission::DASHBOARD_ORDERS_STATUS,

                Permission::ANALYSIS_VIEW,
                Permission::ANALYSIS_ATTENDANCE,
                Permission::ANALYSIS_REVENUE,
                Permission::ANALYSIS_PROFIT_ORDERS,
                Permission::ANALYSIS_TOP_CUSTOMERS,
                Permission::ANALYSIS_TOP_PRODUCTS,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,
                Permission::ATTENDANCES_DELETE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::CUSTOMERS_VIEW,
                Permission::CUSTOMERS_CREATE,
                Permission::CUSTOMERS_UPDATE,
                Permission::CUSTOMERS_DELETE,

                Permission::PRODUCTS_VIEW,

                Permission::ORDERS_VIEW,
                Permission::ORDERS_CREATE,
                Permission::ORDERS_UPDATE,
                Permission::ORDERS_DELETE,
                Permission::ORDERS_SEND,
                Permission::ORDERS_COMPLETE,
                Permission::ORDERS_CANCEL,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
                Permission::PAYROLL_ADJUST,
            ],

            self::ADMIN_BAHAN_BAKU => [
                Permission::DASHBOARD_VIEW,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::SUPPLIERS_VIEW,
                Permission::SUPPLIERS_CREATE,
                Permission::SUPPLIERS_UPDATE,
                Permission::SUPPLIERS_DELETE,

                Permission::RAW_MATERIALS_VIEW,
                Permission::RAW_MATERIALS_CREATE,
                Permission::RAW_MATERIALS_UPDATE,
                Permission::RAW_MATERIALS_DELETE,
                Permission::RAW_MATERIALS_TOGGLE_STATUS,

                Permission::OWNER_VERIFICATIONS_VIEW,

                Permission::PRODUCTS_VIEW,
                Permission::PRODUCTS_CREATE,
                Permission::PRODUCTS_UPDATE,
                Permission::PRODUCTS_DELETE,
                Permission::PRODUCTS_TOGGLE_STATUS,

                Permission::CUTTINGS_VIEW,
                Permission::CUTTINGS_CREATE,
                Permission::CUTTINGS_UPDATE,
                Permission::CUTTINGS_DELETE,
                Permission::CUTTINGS_COMPLETE,
                Permission::CUTTINGS_VERIFY,

                Permission::PURCHASES_VIEW,
                Permission::PURCHASES_CREATE,
                Permission::PURCHASES_UPDATE,
                Permission::PURCHASES_DELETE,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
            ],

            self::MARKETING_OFFLINE, self::MARKETING_ONLINE => [
                Permission::DASHBOARD_VIEW,
                Permission::DASHBOARD_ATTENDANCE,
                Permission::DASHBOARD_REVENUE,
                Permission::DASHBOARD_ORDERS_CHANNEL,
                Permission::DASHBOARD_ORDERS_PAYMENT,
                Permission::DASHBOARD_ORDERS_MARKETING,
                Permission::DASHBOARD_ORDERS_STATUS,

                Permission::ANALYSIS_VIEW,
                Permission::ANALYSIS_ATTENDANCE,
                Permission::ANALYSIS_REVENUE,
                Permission::ANALYSIS_PROFIT_ORDERS,
                Permission::ANALYSIS_TOP_PRODUCTS,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::PRODUCTS_VIEW,

                Permission::ORDERS_VIEW,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
            ],

            self::NON_OPERATOR => [
                Permission::DASHBOARD_VIEW,
                Permission::DASHBOARD_ATTENDANCE,

                Permission::ANALYSIS_VIEW,
                Permission::ANALYSIS_ATTENDANCE,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
            ],

            self::STOK_OPNAME => [
                Permission::DASHBOARD_VIEW,
                Permission::DASHBOARD_ATTENDANCE,

                Permission::ATTENDANCES_VIEW,
                Permission::ATTENDANCES_CREATE,

                Permission::LEAVE_REQUESTS_VIEW,
                Permission::LEAVE_REQUESTS_CREATE,
                Permission::LEAVE_REQUESTS_UPDATE,
                Permission::LEAVE_REQUESTS_DELETE,

                Permission::PRODUCTS_VIEW,

                Permission::STOK_OPNAMES_VIEW,
                Permission::STOK_OPNAMES_CREATE,
                Permission::STOK_OPNAMES_UPDATE,
                Permission::STOK_OPNAMES_DELETE,
                Permission::STOK_OPNAMES_SUBMIT,

                Permission::EMPLOYEE_ADVANCES_VIEW,
                Permission::EMPLOYEE_ADVANCES_CREATE,
                Permission::EMPLOYEE_ADVANCES_UPDATE,
                Permission::EMPLOYEE_ADVANCES_DELETE,
                Permission::EMPLOYEE_ADVANCES_PAY,

                Permission::PAYROLL_VIEW,
            ],
        };
    }

    public function isAssignable(): bool
    {
        return $this !== self::DEVELOPER;
    }

    /**
     * @return list<array{value: string, label: string}>
     */
    public static function assignableSelectOptions(): array
    {
        return collect(self::cases())
            ->filter(fn (self $role) => $role->isAssignable())
            ->map(fn (self $role) => [
                'value' => $role->value,
                'label' => $role->label(),
            ])
            ->values()
            ->all();
    }

    /**
     * @return list<string>
     */
    public static function assignableValues(): array
    {
        return collect(self::cases())
            ->filter(fn (self $role) => $role->isAssignable())
            ->map(fn (self $role) => $role->value)
            ->values()
            ->all();
    }

    /**
     * @return list<string>
     */
    public static function values(): array
    {
        return array_column(self::cases(), 'value');
    }
}
