// lib/screens/settings/api_ftp_configurations_settings.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:environment_monitoring_app/auth_provider.dart';

class ApiFtpConfigurationsSettingsScreen extends StatelessWidget {
  const ApiFtpConfigurationsSettingsScreen({super.key});

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(8.0, 24.0, 8.0, 16.0),
      child: Text(
        title,
        style: Theme.of(context)
            .textTheme
            .headlineSmall
            ?.copyWith(fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget _buildExpansionTile({
    required String title,
    required IconData leadingIcon,
    required Widget child,
    bool initiallyExpanded = false,
  }) {
    return ExpansionTile(
      leading: Icon(leadingIcon),
      title: Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
      initiallyExpanded: initiallyExpanded,
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
          child: child,
        ),
      ],
    );
  }

  Widget _buildInfoList(List<Map<String, dynamic>>? items,
      Widget Function(Map<String, dynamic>) itemBuilder) {
    if (items == null || items.isEmpty) {
      return const ListTile(
        title: Text('No data available. Sync to download.'),
        dense: true,
      );
    }
    return Column(
      children: items.map((item) => itemBuilder(item)).toList(),
    );
  }

  Widget _buildKeyValueEntry(
      Map<String, dynamic> item, String key1, String key2) {
    return ListTile(
      contentPadding:
      const EdgeInsets.symmetric(horizontal: 24.0, vertical: 4.0),
      title:
      Text(item[key1]?.toString() ?? 'N/A', style: const TextStyle(fontSize: 14)),
      subtitle:
      Text(item[key2]?.toString() ?? 'N/A', style: const TextStyle(fontSize: 12)),
      dense: true,
    );
  }

  Widget _buildFtpConfigEntry(Map<String, dynamic> item) {
    return ListTile(
      contentPadding:
      const EdgeInsets.symmetric(horizontal: 24.0, vertical: 4.0),
      title: Text(item['config_name']?.toString() ?? 'N/A',
          style: const TextStyle(fontSize: 14)),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('Module: ${item['ftp_module']?.toString() ?? 'N/A'}',
              style: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold)),
          Text('Host: ${item['ftp_host']?.toString() ?? 'N/A'}',
              style: const TextStyle(fontSize: 12)),
          Text('User: ${item['ftp_user']?.toString() ?? 'N/A'}',
              style: const TextStyle(fontSize: 12)),
          Text('Pass: ${item['ftp_pass']?.toString() ?? 'N/A'}',
              style: const TextStyle(fontSize: 12)),
          Text('Port: ${item['ftp_port']?.toString() ?? 'N/A'}',
              style: const TextStyle(fontSize: 12)),
        ],
      ),
      leading: const Icon(Icons.folder_shared_outlined),
      dense: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("API/FTP Configurations"),
      ),
      body: Consumer<AuthProvider>(
        builder: (context, auth, child) {
          final apiConfigs = auth.apiConfigs;
          final ftpConfigs = auth.ftpConfigs;

          return ListView(
            padding: const EdgeInsets.all(16.0),
            children: [
              _buildSectionHeader(context, "Server Configurations"),
              Card(
                margin: EdgeInsets.zero,
                child: Column(
                  children: [
                    _buildExpansionTile(
                      title: 'API Configurations',
                      leadingIcon: Icons.cloud,
                      initiallyExpanded: true,
                      child: _buildInfoList(
                          apiConfigs,
                              (item) =>
                              _buildKeyValueEntry(item, 'config_name', 'api_url')),
                    ),
                    _buildExpansionTile(
                      title: 'FTP Configurations',
                      leadingIcon: Icons.folder,
                      initiallyExpanded: true,
                      child: _buildInfoList(
                          ftpConfigs, (item) => _buildFtpConfigEntry(item)),
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}