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

/// Gemini API使用制限の警告ダイアログ
class QuotaWarningDialog extends StatelessWidget {
  const QuotaWarningDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      icon: const Icon(LucideIcons.alertTriangle, color: Colors.orange, size: 48),
      title: const Text(
        'AI使用制限について',
        textAlign: TextAlign.center,
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              'Gemini AI無料版の制限:',
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
            ),
            const SizedBox(height: 8),
            _buildLimitItem('最大15回/分', 'RPM (Requests Per Minute)'),
            _buildLimitItem('最大1,500回/日', 'RPD (Requests Per Day)'),
            _buildLimitItem('最大100万トークン/分', 'TPM (画像1枚≒数万トークン)'),
            const SizedBox(height: 16),
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.orange.withValues(alpha: 0.1),
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: Colors.orange.withValues(alpha: 0.3)),
              ),
              child: const Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Icon(LucideIcons.lightbulb, size: 16, color: Colors.orange),
                      SizedBox(width: 8),
                      Text(
                        '推奨事項',
                        style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13),
                      ),
                    ],
                  ),
                  SizedBox(height: 8),
                  Text(
                    '• 解析は5秒以上間隔を空けてください\n'
                    '• 同じ画像を複数回解析しないでください\n'
                    '• エラーが出たら1〜2分待ってください',
                    style: TextStyle(fontSize: 12, height: 1.5),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 16),
            const Text(
              '※ 新しいAPIキーでも同じIPアドレスから利用する場合、制限が共有される可能性があります。',
              style: TextStyle(fontSize: 11, color: Colors.grey, height: 1.4),
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text('閉じる'),
        ),
      ],
    );
  }

  Widget _buildLimitItem(String value, String description) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4.0),
      child: Row(
        children: [
          const Icon(LucideIcons.checkCircle2, size: 16, color: Colors.green),
          const SizedBox(width: 8),
          Expanded(
            child: RichText(
              text: TextSpan(
                style: const TextStyle(fontSize: 12, color: Colors.black87),
                children: [
                  TextSpan(
                    text: value,
                    style: const TextStyle(fontWeight: FontWeight.bold),
                  ),
                  TextSpan(
                    text: ' - $description',
                    style: const TextStyle(color: Colors.grey),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

/// API制限警告を表示する便利メソッド
void showQuotaWarning(BuildContext context) {
  showDialog(
    context: context,
    builder: (context) => const QuotaWarningDialog(),
  );
}
