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

class ReportKidsViewPage extends StatelessWidget {
  late final Stream<QuerySnapshot> _reportsStream;
  late String sid;

  ReportKidsViewPage({required this.sid}) {
    initializeStreams();
  }

  void initializeStreams() {
    _reportsStream = FirebaseFirestore.instance
        .collection('reports')
        .where('sId', isEqualTo: sid)
        .snapshots();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(' '),
        actions: [
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(right: 16),
            child: Text(
              'Student Report',
              style: TextStyle(
                color: Color(0xFF554994),
                fontSize: 24,
                fontFamily: 'Poppins',
                fontWeight: FontWeight.w700,
                height: 0.04,
                letterSpacing: -0.96,
              ),
            ),
          ),
        ],
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: _reportsStream,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          }

          if (snapshot.hasError) {
            return Center(child: Text('Error: ${snapshot.error}'));
          }

          var docs = snapshot.data!.docs;
          if (docs.isEmpty) {
            return Center(child: Text('No report data available'));
          }

          return ListView.builder(
            itemCount: docs.length,
            itemBuilder: (context, index) {
              var document = docs[index].data() as Map<String, dynamic>;
              return _buildReportItem('Report ${index + 1}', document);
            },
          );
        },
      ),
    );
  }

Widget _buildReportItem(String title, Map<String, dynamic> data) {
  // Exclude the 'sId' field from the data
  data.remove('sId');

  return Card(
    margin: EdgeInsets.symmetric(vertical: 8, horizontal: 14),
    child: Padding(
      padding: EdgeInsets.all(8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: 8),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: data.entries.map((entry) {
              return Padding(
                padding: EdgeInsets.symmetric(vertical: 4),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      entry.key,
                      style: TextStyle(
                        // fontWeight: FontWeight.bold,
                      ),
                    ),
                    Text(entry.value ?? 'Not provided'),
                  ],
                ),
              );
            }).toList(),
          ),
        ],
      ),
    ),
  );
}
}