// ignore_for_file: use_build_context_synchronously

import 'package:academy_lms_app/constants.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/customer_support_provider.dart';
import 'create_customer_support_ticket_screen.dart';
import 'customer_ticket_details_screen.dart';

class CustomerSupportScreen extends StatefulWidget {
  const CustomerSupportScreen({super.key});

  @override
  State<CustomerSupportScreen> createState() => _CustomerSupportScreenState();
}

class _CustomerSupportScreenState extends State<CustomerSupportScreen> {
  @override
  void initState() {
    super.initState();
    Future.microtask(
      () => Provider.of<CustomerSupportProvider>(
        context,
        listen: false,
      ).fetchCustomerSupport(),
    );
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<CustomerSupportProvider>(context);

    if (provider.isLoading) {
      return const Scaffold(body: Center(child: CircularProgressIndicator()));
    }

    if (provider.items.isEmpty) {
      return const Scaffold(
        body: Center(child: Text("No Support Tickets Found")),
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Customer Support',
          style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
        ),

        actions: [
          PopupMenuTheme(
            data: PopupMenuThemeData(
              elevation: 0.5, // shadow remove
              menuPadding: EdgeInsets.zero,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4),
              ),
            ),
            child: PopupMenuButton<String>(
              icon: const Icon(Icons.more_vert, color: Colors.black),
              onSelected: (value) async {
                if (value == 'add') {
                  final result = await Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => const CreateSupportTicketScreen(),
                    ),
                  );

                  if (result == true) {
                    await Provider.of<CustomerSupportProvider>(
                      context,
                      listen: false,
                    ).fetchCustomerSupport();
                  }
                }
              },
              itemBuilder: (context) => const [
                PopupMenuItem(
                  value: 'add',
                  height: 45,
                  child: Center(
                    child: Text('Add New', style: TextStyle(fontSize: 14)),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),

      backgroundColor: kBackGroundColor,
      body: ListView.builder(
        padding: const EdgeInsets.all(16),
        itemCount: provider.items.length,
        itemBuilder: (context, index) {
          final customerSupport = provider.items[index];

          return Material(
            color: Colors.transparent,
            child: InkWell(
              borderRadius: BorderRadius.circular(8),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (_) =>
                        TicketDetailsScreen(ticketCode: customerSupport.code!),
                  ),
                );
              },
              child: Container(
                margin: const EdgeInsets.only(bottom: 16),
                padding: const EdgeInsets.all(10),
                decoration: BoxDecoration(
                  border: Border.all(color: kAppButtonBorderColor),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Text(
                            "Subject",
                            style: const TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ),
                        Text(
                          "Status",
                          style: const TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                    SizedBox(height: 6),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Text(
                            customerSupport.subject ?? '',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: const TextStyle(fontWeight: FontWeight.w400),
                          ),
                        ),

                        Text(
                          customerSupport.status ?? '',
                          style: const TextStyle(fontWeight: FontWeight.w400),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
