import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:sigap/src/features/daily-ops/data/models/models/officers_model.dart';

class OfficerProfileDetail extends StatelessWidget {
  final OfficerModel officer;

  const OfficerProfileDetail({super.key, required this.officer});

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

    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 8, 20, 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 10),
          
          Text('Official Information', style: theme.textTheme.titleLarge),
          
          const SizedBox(height: 16),
          
          // Officer information rows
          if (officer.nrp != null)
            _buildInfoRow(context, 'NRP',
              officer.nrp.toString()),

          if (officer.rank != null && officer.rank!.isNotEmpty)
            _buildInfoRow(context, 'Rank', officer.rank!),

          if (officer.position != null && officer.position!.isNotEmpty)
            _buildInfoRow(context, 'Position', officer.position!),

          if (officer.email != null && officer.email!.isNotEmpty)
            _buildInfoRow(context, 'Email', officer.email!),

          if (officer.phone != null && officer.phone!.isNotEmpty)
            _buildInfoRow(context, 'Phone', officer.phone!),

          if (officer.dateOfBirth != null)
            _buildInfoRow(
              context,
              'Date of Birth',
              DateFormat('dd MMMM yyyy').format(officer.dateOfBirth!),
            ),

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

          if (officer.unitId != null && officer.unitId!.isNotEmpty)
            _buildInfoRow(context, 'Unit ID', officer.unitId!),
          
          if (officer.patrolUnitId != null && officer.patrolUnitId!.isNotEmpty)
            _buildInfoRow(context, 'Patrol Unit', officer.patrolUnitId!),

          if (officer.validUntil != null)
            _buildInfoRow(
              context,
              'Valid Until',
              DateFormat('dd MMMM yyyy').format(officer.validUntil!),
            ),

          if (officer.role != null)
            _buildInfoRow(context, 'Role', _formatRoleName(officer.role!.name)),

          if (officer.createdAt != null)
            _buildInfoRow(
              context,
              'Registered On',
              DateFormat('dd MMMM yyyy').format(officer.createdAt!),
            ),

          // Status information if banned
          if (officer.isBanned) ...[
            const SizedBox(height: 24),
            
            Row(
              children: [
                Icon(Icons.warning_amber_rounded, color: Colors.red, size: 20),
                const SizedBox(width: 8),
                Text('Status Information', style: theme.textTheme.titleLarge),
              ],
            ),
            
            const SizedBox(height: 16),
            
            _buildInfoRow(
              context,
              'Status',
              'Account Suspended',
              valueColor: Colors.red,
            ),

            _buildInfoRow(
              context,
              'Reason',
              officer.bannedReason ?? 'No reason provided',
            ),

            if (officer.bannedUntil != null)
              _buildInfoRow(
                context,
                'Suspended Until',
                DateFormat('dd MMMM yyyy, HH:mm').format(officer.bannedUntil!),
              ),
          ],
          
          // Patrol Stats
          if (officer.panicStrike > 0 || officer.spoofingAttempts > 0) ...[
            const SizedBox(height: 24),
            Text('Patrol Statistics', style: theme.textTheme.titleLarge),

            const SizedBox(height: 16),

            if (officer.panicStrike > 0)
              _buildInfoRow(
                context,
                'Panic Button Strike',
                officer.panicStrike.toString(),
                valueColor: officer.panicStrike > 2 ? Colors.orange : null,
              ),

            if (officer.spoofingAttempts > 0)
              _buildInfoRow(
                context,
                'Spoofing Attempts',
                officer.spoofingAttempts.toString(),
                valueColor: Colors.red,
              ),
          ],
        ],
      ),
    );
  }

  Widget _buildInfoRow(
    BuildContext context,
    String label,
    String value, {
    Color? valueColor,
  }) {
    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?.copyWith(color: valueColor),
          ),
          const SizedBox(height: 8),
          Divider(height: 1, color: theme.dividerColor.withOpacity(0.3)),
        ],
      ),
    );
  }
  
  // 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(' ');
  }
}
