import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../../app/theme.dart';
import '../../shared/models/match.dart';
import '../../shared/regia_share.dart';

/// Azioni per riprendere una diretta interrotta (crash, chiusura app, ecc.).
void showResumeSessionSheet(
  BuildContext context,
  WidgetRef ref, {
  required MatchModel match,
}) {
  final sessionId = match.activeSessionId;
  if (sessionId == null) return;

  showModalBottomSheet<void>(
    context: context,
    backgroundColor: AppTheme.surface,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
    ),
    builder: (ctx) {
      return SafeArea(
        child: Padding(
          padding: const EdgeInsets.fromLTRB(20, 16, 20, 12),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                'Diretta in corso',
                style: Theme.of(ctx).textTheme.titleLarge,
              ),
              const SizedBox(height: 4),
              Text(
                '${match.teamName} vs ${match.opponentName}',
                style: Theme.of(ctx).textTheme.bodyMedium,
              ),
              if (match.activeSessionStatus != null) ...[
                const SizedBox(height: 8),
                Text(
                  'Stato: ${match.activeSessionStatus}',
                  style: Theme.of(ctx).textTheme.labelLarge?.copyWith(
                        color: AppTheme.primaryRed,
                      ),
                ),
              ],
              const SizedBox(height: 20),
              if (match.canResumeCamera)
                FilledButton.icon(
                  onPressed: () {
                    Navigator.pop(ctx);
                    context.push('/session/$sessionId/camera');
                  },
                  icon: const Icon(Icons.videocam),
                  label: const Text('Riprendi camera e trasmetti'),
                  style: FilledButton.styleFrom(
                    backgroundColor: AppTheme.primaryRed,
                    padding: const EdgeInsets.symmetric(vertical: 14),
                  ),
                ),
              if (match.activeSessionStatus == 'idle') ...[
                const SizedBox(height: 8),
                OutlinedButton.icon(
                  onPressed: () {
                    Navigator.pop(ctx);
                    context.push('/setup/${match.id}/1');
                  },
                  icon: const Icon(Icons.settings),
                  label: const Text('Continua configurazione'),
                ),
              ],
              const SizedBox(height: 8),
              OutlinedButton.icon(
                onPressed: () {
                  shareRegiaLink(
                    ctx,
                    ref,
                    sessionId: sessionId,
                    shareSubject:
                        'Regia — ${match.teamName} vs ${match.opponentName}',
                  );
                },
                icon: const Icon(Icons.share),
                label: const Text('Condividi link regia'),
              ),
              const SizedBox(height: 8),
              TextButton(
                onPressed: () => Navigator.pop(ctx),
                child: const Text('Annulla'),
              ),
            ],
          ),
        ),
      );
    },
  );
}
