'use client';

import AdminLayout from '@admin/layouts/admin/admin-layout';
import HeaderWithBackIcon from '@admin/components/header-with-backicon';
import { Card, CardContent, CardHeader, CardTitle } from '@admin/components/ui/card';
import { Head, usePage } from '@inertiajs/react';
import { ChevronRight } from 'lucide-react';
import { Checkbox } from '@admin/components/ui/checkbox';

export default function ViewRole() {
    const { role } = usePage<any>().props;

    // Group permissions by group_name
    const groupedPermissions = (role?.permissions || []).reduce(
        (acc: Record<string, any[]>, permission: any) => {
            const group = permission.group_name || 'Other';
            if (!acc[group]) acc[group] = [];
            acc[group].push(permission);
            return acc;
        },
        {}
    );

    return (
        <>
            <Head title={`View Role: ${role?.name}`} />
            <div className="mx-auto flex w-full flex-1 flex-col gap-4 p-3 sm:gap-6 sm:p-2">
                {/* Header */}
                <div className="flex items-center justify-between">
                    <HeaderWithBackIcon
                        title="View Role"
                        description={`Details and permissions for ${role?.name}`}
                    />
                    <div className="hidden items-center space-x-2 text-sm text-gray-500 sm:flex">
                        <span>Roles</span>
                        <ChevronRight className="h-4 w-4" />
                        <span className="font-medium text-primary">View</span>
                    </div>
                </div>

                {/* Role details */}
                <div className="bg-white p-4 rounded-lg shadow mt-4">
                    <div className="mb-6 grid gap-4 sm:grid-cols-2">
                        <div>
                            <label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-1">
                                Role Name
                            </label>
                            <input
                                type="text"
                                id="name"
                                value={role?.name || ''}
                                readOnly
                                disabled
                                className="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-50 text-gray-700"
                            />
                        </div>
                        <div>
                            <label htmlFor="slug" className="block text-sm font-medium text-gray-700 mb-1">
                                Role Slug
                            </label>
                            <input
                                type="text"
                                id="slug"
                                value={role?.slug || ''}
                                readOnly
                                disabled
                                className="w-full px-3 py-2 border border-gray-300 rounded-md bg-gray-50 text-gray-700"
                            />
                        </div>
                    </div>

                    {/* Permissions groups */}
                    <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
                        {Object.entries(groupedPermissions).map(([group, permissions]) => (
                            <Card key={group} className="border shadow-sm">
                                <CardHeader className="p-4 pb-3">
                                    <CardTitle className="capitalize text-base font-semibold text-gray-800">{group}</CardTitle>
                                </CardHeader>
                                <CardContent className="grid grid-cols-1 gap-2.5 p-4 pt-0">
                                    {permissions.map((p: any) => (
                                        <div key={p.id} className="flex items-center space-x-2">
                                            <Checkbox
                                                checked={true}
                                                disabled
                                                className="pointer-events-none"
                                            />
                                            <label className="text-sm font-normal text-gray-700">
                                                {p.name}
                                            </label>
                                        </div>
                                    ))}
                                </CardContent>
                            </Card>
                        ))}
                    </div>
                </div>
            </div>
        </>
    );
}

ViewRole.layout = (page: React.ReactNode) => (
    <AdminLayout
        breadcrumbs={[
            { title: 'Dashboard', href: route('admin.dashboard') },
            { title: 'Roles', href: route('admin.roles.index') },
            { title: 'View', href: '#' },
        ]}
    >
        {page}
    </AdminLayout>
);
