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

class FeedView extends StatefulWidget {
  const FeedView({super.key});
  @override
  State<FeedView> createState() => _FeedViewState();
}

class _FeedViewState extends State<FeedView> {
  static const Color terminalGreen = Color(0xFFE87D25);
  int _userTier = 1;

  @override
  void initState() {
    super.initState();
    _fetchUserTier();
  }

  Future<void> _fetchUserTier() async {
    final user = FirebaseAuth.instance.currentUser;
    if (user != null) {
      final doc = await FirebaseFirestore.instance.collection('users').doc(user.uid).get();
      if (mounted) setState(() => _userTier = doc.data()?['tier'] ?? 1);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 220,
          child: PageView(
            children: [
              _buildPromoCard('assets/images/promo1.jpg', "SIGNAL: HIGH", "SECTOR: CHI_064"),
              _buildPromoCard('assets/images/promo2.jpg', "STATUS: ENCRYPTED", "TIER_ACCESS: ENABLED"),
            ],
          ),
        ),
        Expanded(
          child: StreamBuilder<QuerySnapshot>(
            stream: FirebaseFirestore.instance.collection('posts').orderBy('timestamp', descending: true).snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) return const Center(child: CircularProgressIndicator(color: terminalGreen));
              final docs = snapshot.data!.docs;

              return ListView.builder(
                padding: const EdgeInsets.only(top: 10, bottom: 20),
                itemCount: docs.length,
                itemBuilder: (context, index) {
                  final post = docs[index].data() as Map<String, dynamic>;
                  final int postMinTier = post['minTier'] ?? 1;

                  // LOCK LOGIC
                  if (_userTier < postMinTier) {
                    return _buildRedactedLog(postMinTier);
                  }

                  return _buildLogEntry(context, post, docs[index].id, (post['timestamp'] as Timestamp?)?.toDate() ?? DateTime.now());
                },
              );
            },
          ),
        ),
      ],
    );
  }

  // THE LOCKED VIEW
  Widget _buildRedactedLog(int requiredTier) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.red.withValues(alpha: 0.3)),
        color: Colors.red.withValues(alpha: 0.05),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text("LOG_STATUS: [CLASSIFIED_DATA]", style: TextStyle(color: Colors.red, fontSize: 9)),
          const SizedBox(height: 8),
          const Text("X X X X X X X X X X X X X X X X", style: TextStyle(color: Colors.white10, letterSpacing: 2)),
          const SizedBox(height: 5),
          const Text("REDACTED REDACTED REDACTED REDACTED REDACTED", style: TextStyle(color: Colors.white10)),
          const SizedBox(height: 10),
          Text("[ ACCESS_LEVEL_0${requiredTier}_REQUIRED ]", style: const TextStyle(color: Colors.red, fontSize: 10, fontWeight: FontWeight.bold)),
        ],
      ),
    );
  }

  Widget _buildLogEntry(BuildContext context, Map data, String id, DateTime date) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(border: Border.all(color: terminalGreen.withValues(alpha: 0.2)), color: Colors.black),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text("LOG_ID: ${id.substring(0, 8)} | ${DateFormat('yy.MM.dd').format(date)}", style: const TextStyle(fontSize: 9, color: Colors.white24)),
          const SizedBox(height: 8),
          Text(data['title'].toString().toUpperCase(), style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16, letterSpacing: 1.5)),
          const SizedBox(height: 8),
          Text(data['content'], style: const TextStyle(color: Colors.white70, fontSize: 13, height: 1.4)),
        ],
      ),
    );
  }

  Widget _buildPromoCard(String path, String t1, String t2) {
    return Container(
      margin: const EdgeInsets.all(10),
      decoration: BoxDecoration(
        border: Border.all(color: terminalGreen.withValues(alpha: 0.4)),
        image: DecorationImage(image: AssetImage(path), fit: BoxFit.cover, colorFilter: ColorFilter.mode(Colors.green.withValues(alpha: 0.15), BlendMode.srcATop)),
      ),
      child: Container(
        color: Colors.black.withValues(alpha: 0.3),
        padding: const EdgeInsets.all(12),
        alignment: Alignment.bottomLeft,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(t1, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14, letterSpacing: 1)),
            Text(t2, style: TextStyle(color: terminalGreen.withValues(alpha: 0.7), fontSize: 10)),
          ],
        ),
      ),
    );
  }
}