<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class DeveloperControlCenterController extends Controller
{
    /**
     * Halaman pusat kontrol developer.
     *
     * Catatan:
     * - Batasi akses hanya untuk role developer / superadmin.
     * - Jangan expose halaman ini ke user biasa.
     */
    public function index(Request $request)
    {
        abort_unless($this->canAccessDeveloperCenter($request->user()), 403);

        $modules = [
            [
                'title' => 'Transaksi Order',
                'description' => 'Akses penuh untuk melihat, edit, ubah status, dan hapus transaksi order.',
                'icon' => 'receipt',
                'items' => [
                    ['label' => 'Semua Order', 'url' => route_exists('orders.index') ? route('orders.index') : url('/orders'), 'style' => 'primary'],
                    ['label' => 'Buat Order', 'url' => route_exists('orders.create') ? route('orders.create') : url('/orders/create'), 'style' => 'success'],
                    ['label' => 'Cari & Edit', 'url' => route_exists('orders.index') ? route('orders.index') . '?mode=edit' : url('/orders?mode=edit'), 'style' => 'warning'],
                    ['label' => 'Cari & Hapus', 'url' => route_exists('orders.index') ? route('orders.index') . '?mode=delete' : url('/orders?mode=delete'), 'style' => 'danger'],
                ],
            ],
            [
                'title' => 'Pelanggan',
                'description' => 'Kelola semua customer, riwayat, dan data identitas.',
                'icon' => 'users',
                'items' => [
                    ['label' => 'Semua Pelanggan', 'url' => url('/customers'), 'style' => 'primary'],
                    ['label' => 'Tambah Pelanggan', 'url' => url('/customers/create'), 'style' => 'success'],
                    ['label' => 'Edit Data', 'url' => url('/customers?mode=edit'), 'style' => 'warning'],
                    ['label' => 'Hapus Data', 'url' => url('/customers?mode=delete'), 'style' => 'danger'],
                ],
            ],
            [
                'title' => 'Users & Role',
                'description' => 'Kontrol penuh user, role, permission, dan reset akses.',
                'icon' => 'shield',
                'items' => [
                    ['label' => 'Kelola Users', 'url' => url('/users'), 'style' => 'primary'],
                    ['label' => 'Tambah User', 'url' => url('/users/create'), 'style' => 'success'],
                    ['label' => 'Roles', 'url' => url('/roles'), 'style' => 'warning'],
                    ['label' => 'Permissions', 'url' => url('/permissions'), 'style' => 'danger'],
                ],
            ],
            [
                'title' => 'Produk / Layanan',
                'description' => 'Edit master produk, harga, kategori, dan variasi layanan.',
                'icon' => 'box',
                'items' => [
                    ['label' => 'Semua Produk', 'url' => url('/products'), 'style' => 'primary'],
                    ['label' => 'Tambah Produk', 'url' => url('/products/create'), 'style' => 'success'],
                    ['label' => 'Edit Produk', 'url' => url('/products?mode=edit'), 'style' => 'warning'],
                    ['label' => 'Hapus Produk', 'url' => url('/products?mode=delete'), 'style' => 'danger'],
                ],
            ],
            [
                'title' => 'Keuangan & Rekening',
                'description' => 'Akses rekening pembayaran, kas, deposit, dan modul finance.',
                'icon' => 'wallet',
                'items' => [
                    ['label' => 'Payment Accounts', 'url' => url('/payment-accounts'), 'style' => 'primary'],
                    ['label' => 'Finance', 'url' => url('/finance'), 'style' => 'success'],
                    ['label' => 'Deposits', 'url' => url('/finance/deposits'), 'style' => 'warning'],
                    ['label' => 'Cash Ledger', 'url' => url('/reports/cash-ledger'), 'style' => 'danger'],
                ],
            ],
            [
                'title' => 'System Settings',
                'description' => 'Edit logo, identitas toko, pengaturan umum, dan konfigurasi sistem.',
                'icon' => 'settings',
                'items' => [
                    ['label' => 'Quick Settings', 'url' => route_exists('settings.index') ? route('settings.index') : url('/settings'), 'style' => 'primary'],
                    ['label' => 'Logo & Branding', 'url' => route_exists('settings.index') ? route('settings.index') . '#branding' : url('/settings#branding'), 'style' => 'success'],
                    ['label' => 'Backup DB', 'url' => url('/backups'), 'style' => 'warning'],
                    ['label' => 'Tutorial / Docs', 'url' => url('/tutorial'), 'style' => 'danger'],
                ],
            ],
        ];

        $dangerActions = [
            [
                'title' => 'Masuk ke semua transaksi',
                'description' => 'Akses semua transaksi dalam mode kontrol penuh.',
                'url' => url('/orders?developer_full_access=1'),
                'button' => 'Buka transaksi penuh',
            ],
            [
                'title' => 'Masuk ke semua data master',
                'description' => 'Kelola customer, produk, kategori, dan akun dari satu jalur cepat.',
                'url' => url('/developer/control-center?section=master-data'),
                'button' => 'Buka data master',
            ],
            [
                'title' => 'Bypass halaman ringkasan',
                'description' => 'Langsung masuk ke halaman kerja yang biasa dipakai developer.',
                'url' => route_exists('settings.index') ? route('settings.index') : url('/settings'),
                'button' => 'Masuk ke settings',
            ],
        ];

        $quickLookup = [
            'order' => [
                'title' => 'Cari Order',
                'placeholder' => 'Masukkan invoice / nomor order',
                'base_url' => url('/orders'),
                'param' => 'q',
            ],
            'customer' => [
                'title' => 'Cari Pelanggan',
                'placeholder' => 'Masukkan nama / nomor customer',
                'base_url' => url('/customers'),
                'param' => 'q',
            ],
            'user' => [
                'title' => 'Cari User',
                'placeholder' => 'Masukkan nama / email user',
                'base_url' => url('/users'),
                'param' => 'q',
            ],
        ];

        return view('pages.admin.developer-control-center', [
    'pageTitle' => 'Developer Control Center',
    'modules' => $modules,
    'dangerActions' => $dangerActions,
    'quickLookup' => $quickLookup,
    'userName' => $request->user()?->name ?? 'Developer',
]);
    }

    protected function canAccessDeveloperCenter($user): bool
    {
        if (!$user) {
            return false;
        }

        if (method_exists($user, 'hasRole')) {
            return $user->hasRole('developer') || $user->hasRole('superadmin');
        }

        if (property_exists($user, 'role')) {
            return in_array($user->role, ['developer', 'superadmin'], true);
        }

        return false;
    }
}

if (!function_exists('route_exists')) {
    function route_exists(string $name): bool
    {
        return Route::has($name);
    }
}
