content: import 'dart:convert';
  
  import 'package:flutter/material.dart';
  
  import 'package:kubenav/models/kubernetes/io_k8s_api_core_v1_namespace.dart';
  import 'package:kubenav/models/kubernetes/io_k8s_api_core_v1_namespace_list.dart';
  import 'package:kubenav/models/kubernetes/io_k8s_apimachinery_pkg_apis_meta_v1_condition.dart';
  import 'package:kubenav/models/plugins/prometheus.dart';
  import 'package:kubenav/utils/constants.dart';
  import 'package:kubenav/utils/resources.dart';
  import 'package:kubenav/widgets/resources/helpers/details_item.dart';
  import 'package:kubenav/widgets/resources/helpers/details_item_conditions.dart';
  import 'package:kubenav/widgets/resources/helpers/details_item_metadata.dart';
  import 'package:kubenav/widgets/resources/helpers/details_resources_preview.dart';
  import 'package:kubenav/widgets/resources/resources/resources.dart';
  import 'package:kubenav/widgets/resources/resources/resources_cronjobs.dart';
  import 'package:kubenav/widgets/resources/resources/resources_deployments.dart';
  import 'package:kubenav/widgets/resources/resources/resources_events.dart';
  import 'package:kubenav/widgets/resources/resources/resources_pods.dart';
  import 'package:kubenav/widgets/resources/resources/resources_statefulsets.dart';
  import 'package:kubenav/widgets/resources/resources_list.dart';
  import 'package:kubenav/widgets/shared/app_prometheus_charts_widget.dart';
  
  final resourceNamespace = Resource(
    category: ResourceCategories.cluster,
    plural: 'Namespaces',
    singular: 'Namespace',
    description:
        'Namespaces provides a mechanism for isolating groups of resources within a single cluster.',
    path: '/api/v1',
    resource: 'namespaces',
    scope: ResourceScope.cluster,
    additionalPrinterColumns: [],
    icon: 'namespaces',
    template: resourceDefaultTemplate,
    decodeListData: (ResourcesListData data) {
      final parsed = json.decode(data.list);
      final items = IoK8sApiCoreV1NamespaceList.fromJson(parsed)?.items ?? [];
  
      return items
          .map(
            (e) => ResourceItem(
              item: e,
              metrics: null,
              status: e.status?.phase == 'Active'
                  ? ResourceStatus.success
                  : ResourceStatus.warning,
            ),
          )
          .toList();
    },
    decodeList: (String data) {
      final parsed = json.decode(data);
      return IoK8sApiCoreV1NamespaceList.fromJson(parsed)?.items ?? [];
    },
    getName: (dynamic item) {
      return (item as IoK8sApiCoreV1Namespace).metadata?.name ?? '';
    },
    getNamespace: (dynamic item) {
      return (item as IoK8sApiCoreV1Namespace).metadata?.namespace;
    },
    decodeItem: (String data) {
      final parsed = json.decode(data);
      return IoK8sApiCoreV1Namespace.fromJson(parsed);
    },
    encodeItem: (dynamic item) {
      JsonEncoder encoder = const JsonEncoder.withIndent('  ');
      return encoder.convert(item);
    },
    toJson: (dynamic item) {
      return json.decode(json.encode(item));
    },
    listItemBuilder:
        (BuildContext context, Resource resource, ResourceItem listItem) {
          final item = listItem.item as IoK8sApiCoreV1Namespace;
          final status = listItem.status;
  
          return ResourcesListItem(
            name: item.metadata?.name ?? '',
            namespace: item.metadata?.namespace,
            resource: resource,
            item: item,
            status: status,
            details: [
              'Status: ${item.status?.phase ?? '-'}',
              'Age: ${getAge(item.metadata?.creationTimestamp)}',
            ],
          );
        },
    previewItemBuilder: (dynamic listItem) {
      final item = listItem as IoK8sApiCoreV1Namespace;
  
      return [
        'Status: ${item.status?.phase ?? '-'}',
        'Age: ${getAge(item.metadata?.creationTimestamp)}',
      ];
    },
    detailsItemBuilder: (BuildContext context, Resource resource, dynamic detailsItem) {
      final item = detailsItem as IoK8sApiCoreV1Namespace;
  
      return Column(
        children: [
          DetailsItemMetadata(kind: item.kind, metadata: item.metadata),
          DetailsItemConditions(
            conditions: item.status?.conditions
                .map(
                  (e) => IoK8sApimachineryPkgApisMetaV1Condition(
                    lastTransitionTime: e.lastTransitionTime ?? DateTime.now(),
                    message: e.message ?? '',
                    observedGeneration: null,
                    reason: e.reason ?? '',
                    status: e.status,
                    type: e.type,
                  ),
                )
                .toList(),
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsItem(
            title: 'Status',
            details: [
              DetailsItemModel(name: 'Phase', values: item.status!.phase),
            ],
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsResourcesPreview(
            resource: resourceCronJob,
            namespace: item.metadata?.name,
            selector: '',
            filter: null,
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsResourcesPreview(
            resource: resourceDeployment,
            namespace: item.metadata?.name,
            selector: '',
            filter: null,
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsResourcesPreview(
            resource: resourcePod,
            namespace: item.metadata?.name,
            selector: '',
            filter: null,
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsResourcesPreview(
            resource: resourceStatefulSet,
            namespace: item.metadata?.name,
            selector: '',
            filter: null,
          ),
          const SizedBox(height: Constants.spacingMiddle),
          DetailsResourcesPreview(
            resource: resourceEvent,
            namespace: item.metadata?.namespace,
            selector:
                'fieldSelector=involvedObject.name=${item.metadata?.name ?? ''}',
            filter: null,
          ),
          const SizedBox(height: Constants.spacingMiddle),
          AppPrometheusChartsWidget(
            item: item,
            toJson: resource.toJson,
            defaultCharts: [
              Chart(
                title: 'Total CPU Usage',
                unit: 'm',
                queries: [
                  Query(
                    query:
                        'sum(rate(container_cpu_usage_seconds_total{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", container!="", container!="POD"}[2m])) * 1000',
                    label: 'Usage',
                  ),
                  Query(
                    query:
                        'sum(kube_pod_container_resource_requests{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", resource="cpu", container!="", container!="POD"}) * 1000',
                    label: 'Requests',
                  ),
                  Query(
                    query:
                        'sum(kube_pod_container_resource_limits{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", resource="cpu", container!="", container!="POD"}) * 1000',
                    label: 'Limits',
                  ),
                ],
              ),
              Chart(
                title: 'Total Memory Usage',
                unit: 'MiB',
                queries: [
                  Query(
                    query:
                        '(sum(container_memory_working_set_bytes{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", container!="", container!="POD"})) / 1024 / 1024',
                    label: 'Usage',
                  ),
                  Query(
                    query:
                        '(sum(kube_pod_container_resource_requests{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", resource="memory", container!="", container!="POD"})) / 1024 / 1024',
                    label: 'Requests',
                  ),
                  Query(
                    query:
                        '(sum(kube_pod_container_resource_limits{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", resource="memory", container!="", container!="POD"})) / 1024 / 1024',
                    label: 'Limits',
                  ),
                ],
              ),
              Chart(
                title: 'Total Network I/O',
                unit: 'MiB',
                queries: [
                  Query(
                    query:
                        'sum(rate(container_network_receive_bytes_total{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}"}[2m])) / 1024 / 1024',
                    label: 'Received',
                  ),
                  Query(
                    query:
                        '-sum(rate(container_network_transmit_bytes_total{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}"}[2m])) / 1024 / 1024',
                    label: 'Transmitted',
                  ),
                ],
              ),
              Chart(
                title: 'Total Restarts',
                unit: '',
                queries: [
                  Query(
                    query:
                        'sum(kube_pod_container_status_restarts_total{namespace="{{with .metadata}}{{with .name}}{{.}}{{end}}{{end}}", container!="", container!="POD"}) by (pod)',
                    label: 'Restarts',
                  ),
                ],
              ),
            ],
          ),
        ],
      );
    },
  );
  