import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import '../../common/theme_shadcn.dart';
import '../../../data/models/company/company_dto.dart';
import '../../../injection_container.dart';
import '../controllers/company_controller.dart';

/// 회사 복구 확인 다이얼로그
class CompanyRestoreDialog extends StatefulWidget {
  final CompanyDto company;
  final VoidCallback? onRestored;

  const CompanyRestoreDialog({
    super.key,
    required this.company,
    this.onRestored,
  });

  @override
  State<CompanyRestoreDialog> createState() => _CompanyRestoreDialogState();
}

class _CompanyRestoreDialogState extends State<CompanyRestoreDialog> {
  late final CompanyController _controller;
  bool _isRestoring = false;

  @override
  void initState() {
    super.initState();
    _controller = sl<CompanyController>();
  }

  Future<void> _restore() async {
    setState(() {
      _isRestoring = true;
    });

    final success = await _controller.restoreCompany(widget.company.id!);

    if (mounted) {
      if (success) {
        Navigator.of(context).pop(true);
        if (widget.onRestored != null) {
          widget.onRestored!();
        }
        
        // 성공 메시지
        ShadToaster.of(context).show(
          ShadToast(
            title: const Text('복구 완료'),
            description: Text('${widget.company.name} 회사가 복구되었습니다.'),
          ),
        );
      } else {
        setState(() {
          _isRestoring = false;
        });
        
        // 실패 메시지
        ShadToaster.of(context).show(
          ShadToast.destructive(
            title: const Text('복구 실패'),
            description: Text(_controller.error ?? '회사 복구에 실패했습니다.'),
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return ShadDialog(
      child: SizedBox(
        width: 400,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 헤더
            Row(
              children: [
                Icon(Icons.restore, color: Colors.green, size: 24),
                const SizedBox(width: 12),
                Expanded(
                  child: Text('회사 복구', style: ShadcnTheme.headingH3),
                ),
              ],
            ),
            const SizedBox(height: 24),

            // 복구 정보
            Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: Colors.green.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: Colors.green.withValues(alpha: 0.2)),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '다음 회사를 복구하시겠습니까?',
                    style: ShadcnTheme.bodyLarge.copyWith(fontWeight: FontWeight.w500),
                  ),
                  const SizedBox(height: 12),
                  _buildInfoRow('회사명', widget.company.name),
                  _buildInfoRow('담당자', widget.company.contactName),
                  _buildInfoRow('연락처', widget.company.contactPhone),
                  if (widget.company.contactEmail.isNotEmpty)
                    _buildInfoRow('이메일', widget.company.contactEmail),
                  _buildInfoRow('주소', widget.company.address),
                ],
              ),
            ),
            
            const SizedBox(height: 16),
            
            Text(
              '복구된 회사는 다시 활성 상태로 변경됩니다.',
              style: ShadcnTheme.bodyMedium.copyWith(
                color: ShadcnTheme.foregroundMuted,
              ),
            ),

            const SizedBox(height: 24),

            // 버튼들
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                ShadButton.outline(
                  onPressed: _isRestoring ? null : () => Navigator.of(context).pop(false),
                  child: const Text('취소'),
                ),
                const SizedBox(width: 12),
                ShadButton(
                  onPressed: _isRestoring ? null : _restore,
                  child: _isRestoring
                    ? Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          SizedBox(
                            width: 16,
                            height: 16,
                            child: CircularProgressIndicator(
                              strokeWidth: 2,
                              color: ShadcnTheme.primaryForeground,
                            ),
                          ),
                          const SizedBox(width: 8),
                          const Text('복구 중...'),
                        ],
                      )
                    : const Text('복구'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 2),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 80,
            child: Text(
              '$label:',
              style: ShadcnTheme.bodySmall.copyWith(
                color: ShadcnTheme.foregroundMuted,
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: ShadcnTheme.bodySmall.copyWith(
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

/// 회사 복구 다이얼로그 표시 유틸리티
Future<bool?> showCompanyRestoreDialog(
  BuildContext context, {
  required CompanyDto company,
  VoidCallback? onRestored,
}) async {
  return await showDialog<bool>(
    context: context,
    barrierDismissible: false,
    builder: (context) => CompanyRestoreDialog(
      company: company,
      onRestored: onRestored,
    ),
  );
}
