import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:sigap/src/features/personalization/data/models/models/profile_model.dart';
import 'package:sigap/src/features/personalization/data/models/models/users_model.dart';

class UserProfileDetail extends StatelessWidget {
  final UserModel? user;
  final ProfileModel? profile;

  const UserProfileDetail({super.key, this.user, this.profile});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    // Check if we have user data
    if (user == null && profile == null) {
      return Padding(
        padding: const EdgeInsets.all(20.0),
        child: Center(
          child: Column(
            children: [
              Icon(Icons.person_off_outlined, size: 48, color: theme.hintColor),
              const SizedBox(height: 16),
              Text(
                'User profile information not available',
                style: theme.textTheme.bodyLarge,
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      );
    }

    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 8, 20, 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 10),
          Text('Personal Information', style: theme.textTheme.titleLarge),
          const SizedBox(height: 16),

          // NIK (National ID)
          if (profile?.nik != null && profile!.nik!.isNotEmpty)
            _buildInfoRow(context, 'NIK', profile!.nik!),

          // Email
          if (user?.email != null) _buildInfoRow(context, 'Email', user!.email),

          // Username
          if (profile?.username != null && profile!.username!.isNotEmpty)
            _buildInfoRow(context, 'Username', profile!.username!),

          // Phone
          if (user?.phone != null && user!.phone!.isNotEmpty)
            _buildInfoRow(context, 'Phone', user!.phone!),

          // Birth Date
          if (profile?.birthDate != null)
            _buildInfoRow(
              context,
              'Birth Date',
              DateFormat('dd MMMM yyyy').format(profile!.birthDate!),
            ),

          // Place of Birth
          if (profile?.placeOfBirth != null &&
              profile!.placeOfBirth!.isNotEmpty)
            _buildInfoRow(context, 'Place of Birth', profile!.placeOfBirth!),

          // Address
          if (profile?.address != null &&
              _getDisplayAddress(profile!.address!).isNotEmpty)
            _buildInfoRow(
              context,
              'Address',
              _getDisplayAddress(profile!.address!),
            ),

          // Last Sign In
          if (user?.lastSignInAt != null)
            _buildInfoRow(
              context,
              'Last Sign In',
              DateFormat('dd MMM yyyy, HH:mm').format(user!.lastSignInAt!),
            ),

          // Account Created
          if (user?.createdAt != null)
            _buildInfoRow(
              context,
              'Account Created',
              DateFormat('dd MMMM yyyy').format(user!.createdAt),
            ),

          // Role
          if (user?.role != null)
            _buildInfoRow(
              context,
              'Role',
              _formatRoleName(user!.role!.name ?? 'User'),
            ),
        ],
      ),
    );
  }

  Widget _buildInfoRow(BuildContext context, String label, String value) {
    final theme = Theme.of(context);

    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            label,
            style: theme.textTheme.bodyMedium?.copyWith(
              color: theme.hintColor,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 4),
          Text(value, style: theme.textTheme.titleMedium),
          const SizedBox(height: 8),
          Divider(height: 1, color: theme.dividerColor.withOpacity(0.3)),
        ],
      ),
    );
  }

  // Helper to get displayable address from address map
  String _getDisplayAddress(Map<String, dynamic> addressMap) {
    final fullAddress = addressMap['full_address']?.toString();
    if (fullAddress != null && fullAddress.isNotEmpty) {
      return fullAddress;
    }

    final parts = <String>[];
    if (addressMap['street'] != null) parts.add(addressMap['street']);
    if (addressMap['city'] != null) parts.add(addressMap['city']);
    if (addressMap['state'] != null) parts.add(addressMap['state']);
    if (addressMap['country'] != null) parts.add(addressMap['country']);
    if (addressMap['postal_code'] != null) parts.add(addressMap['postal_code']);

    return parts.join(', ');
  }

  // Format role name for better display
  String _formatRoleName(String roleName) {
    // Convert snake_case or kebab-case to Title Case
    return roleName
        .split(RegExp(r'[_\-]'))
        .map(
          (word) =>
              word.isEmpty
                  ? ''
                  : '${word[0].toUpperCase()}${word.substring(1)}',
        )
        .join(' ');
  }
}
