import {
  Component,
  DestroyRef,
  inject,
  OnInit,
  ChangeDetectionStrategy,
  signal,
  computed,
  Signal,
} from "@angular/core";
import { takeUntilDestroyed, toObservable, toSignal } from "@angular/core/rxjs-interop";
import { FormControl, ReactiveFormsModule } from "@angular/forms";
import { ActivatedRoute } from "@angular/router";
import { combineLatest, debounceTime, EMPTY, map, startWith, switchMap } from "rxjs";

import { Security } from "@bitwarden/assets/svg";
import { RiskInsightsDataService } from "@bitwarden/bit-common/dirt/reports/risk-insights";
import { createNewSummaryData } from "@bitwarden/bit-common/dirt/reports/risk-insights/helpers";
import {
  OrganizationReportSummary,
  ReportStatus,
} from "@bitwarden/bit-common/dirt/reports/risk-insights/models/report-models";
import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { OrganizationId } from "@bitwarden/common/types/guid";
import {
  ButtonModule,
  IconButtonModule,
  LinkModule,
  NoItemsModule,
  SearchModule,
  TableDataSource,
  ToastService,
  TooltipDirective,
  TypographyModule,
  ChipFilterComponent,
  ChipFilterOption,
} from "@bitwarden/components";
import { ExportHelper } from "@bitwarden/vault-export-core";
import { exportToCSV } from "@bitwarden/web-vault/app/dirt/reports/report-utils";
import { HeaderModule } from "@bitwarden/web-vault/app/layouts/header/header.module";
import { SharedModule } from "@bitwarden/web-vault/app/shared";
import { PipesModule } from "@bitwarden/web-vault/app/vault/individual-vault/pipes/pipes.module";

import { AppTableRowScrollableM11Component } from "../shared/app-table-row-scrollable-m11.component";
import { ApplicationTableDataSource } from "../shared/app-table-row-scrollable.component";
import { ReportLoadingComponent } from "../shared/report-loading.component";
import { AccessIntelligenceSecurityTasksService } from "../shared/security-tasks.service";

export const ApplicationFilterOption = {
  All: "all",
  Critical: "critical",
  NonCritical: "nonCritical",
} as const;

export type ApplicationFilterOption =
  (typeof ApplicationFilterOption)[keyof typeof ApplicationFilterOption];

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: "dirt-applications",
  templateUrl: "./applications.component.html",
  imports: [
    ReportLoadingComponent,
    HeaderModule,
    LinkModule,
    SearchModule,
    PipesModule,
    NoItemsModule,
    SharedModule,
    AppTableRowScrollableM11Component,
    IconButtonModule,
    TypographyModule,
    ButtonModule,
    ReactiveFormsModule,
    ChipFilterComponent,
    TooltipDirective,
  ],
})
export class ApplicationsComponent implements OnInit {
  readonly destroyRef = inject(DestroyRef);
  private readonly fileDownloadService = inject(FileDownloadService);
  private readonly logService = inject(LogService);

  protected readonly ReportStatusEnum = ReportStatus;
  protected readonly noItemsIcon = Security;

  // Standard properties
  protected readonly dataSource = new TableDataSource<ApplicationTableDataSource>();
  protected readonly searchControl = new FormControl<string>("", { nonNullable: true });
  protected readonly filteredTableData = toSignal(this.dataSource.connect(), {
    initialValue: [],
  });

  // Template driven properties
  protected readonly selectedUrls = signal(new Set<string>());
  protected readonly updatingCriticalApps = signal(false);
  protected readonly applicationSummary = signal<OrganizationReportSummary>(createNewSummaryData());
  protected readonly criticalApplicationsCount = signal(0);
  protected readonly totalApplicationsCount = signal(0);
  protected readonly nonCriticalApplicationsCount = computed(() => {
    return this.totalApplicationsCount() - this.criticalApplicationsCount();
  });
  protected readonly organizationId = signal<OrganizationId | undefined>(undefined);

  // filter related properties
  protected readonly selectedFilter = signal<ApplicationFilterOption>(ApplicationFilterOption.All);
  protected readonly selectedFilterObservable = toObservable(this.selectedFilter);
  protected readonly ApplicationFilterOption = ApplicationFilterOption;
  protected readonly filterOptions: Signal<ChipFilterOption<string>[]> = computed(() => [
    {
      label: this.i18nService.t("critical", this.criticalApplicationsCount()),
      value: ApplicationFilterOption.Critical,
    },
    {
      label: this.i18nService.t("notCritical", this.nonCriticalApplicationsCount()),
      value: ApplicationFilterOption.NonCritical,
    },
  ]);

  // Computed property that returns only selected applications that are currently visible in filtered data
  readonly visibleSelectedApps = computed(() => {
    const filteredData = this.filteredTableData();
    const selected = this.selectedUrls();

    if (!filteredData || selected.size === 0) {
      return new Set<string>();
    }

    const visibleSelected = new Set<string>();
    filteredData.forEach((row) => {
      if (selected.has(row.applicationName)) {
        visibleSelected.add(row.applicationName);
      }
    });

    return visibleSelected;
  });

  readonly allSelectedAppsAreCritical = computed(() => {
    const visibleSelected = this.visibleSelectedApps();
    const filteredData = this.filteredTableData();

    if (!filteredData || visibleSelected.size === 0) {
      return false;
    }

    return filteredData
      .filter((row) => visibleSelected.has(row.applicationName))
      .every((row) => row.isMarkedAsCritical);
  });

  protected readonly unassignedCipherIds = toSignal(
    this.securityTasksService.unassignedCriticalCipherIds$,
    { initialValue: [] },
  );

  readonly enableRequestPasswordChange = computed(() => this.unassignedCipherIds().length > 0);

  constructor(
    protected readonly i18nService: I18nService,
    protected readonly activatedRoute: ActivatedRoute,
    protected readonly toastService: ToastService,
    protected readonly dataService: RiskInsightsDataService,
    protected readonly securityTasksService: AccessIntelligenceSecurityTasksService,
  ) {}

  async ngOnInit() {
    this.activatedRoute.paramMap
      .pipe(
        takeUntilDestroyed(this.destroyRef),
        map((params) => params.get("organizationId")),
        switchMap(async (orgId) => {
          if (orgId) {
            this.organizationId.set(orgId as OrganizationId);
          } else {
            return EMPTY;
          }
        }),
      )
      .subscribe();

    this.dataService.enrichedReportData$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
      next: (report) => {
        if (report != null) {
          this.applicationSummary.set(report.summaryData);

          // Map the report data to include the iconCipher for each application
          const tableDataWithIcon = report.reportData.map((app) => ({
            ...app,
            iconCipher:
              app.cipherIds.length > 0
                ? this.dataService.getCipherIcon(app.cipherIds[0])
                : undefined,
          }));
          this.dataSource.data = tableDataWithIcon;
          this.totalApplicationsCount.set(report.reportData.length);
          this.criticalApplicationsCount.set(
            report.reportData.filter((app) => app.isMarkedAsCritical).length,
          );
        } else {
          this.dataSource.data = [];
        }
      },
      error: () => {
        this.dataSource.data = [];
      },
    });

    combineLatest([
      this.searchControl.valueChanges.pipe(startWith("")),
      this.selectedFilterObservable,
    ])
      .pipe(debounceTime(200), takeUntilDestroyed(this.destroyRef))
      .subscribe(([searchText, selectedFilter]) => {
        let filterFunction = (app: ApplicationTableDataSource) => true;

        if (selectedFilter === ApplicationFilterOption.Critical) {
          filterFunction = (app) => app.isMarkedAsCritical;
        } else if (selectedFilter === ApplicationFilterOption.NonCritical) {
          filterFunction = (app) => !app.isMarkedAsCritical;
        }

        this.dataSource.filter = (app) =>
          filterFunction(app) &&
          app.applicationName.toLowerCase().includes(searchText.toLowerCase());
      });
  }

  setFilterApplicationsByStatus(value: ApplicationFilterOption) {
    this.selectedFilter.set(value);
  }

  async markAppsAsCritical() {
    this.updatingCriticalApps.set(true);
    const visibleSelected = this.visibleSelectedApps();
    const count = visibleSelected.size;

    this.dataService
      .saveCriticalApplications(Array.from(visibleSelected))
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe({
        next: (response) => {
          this.updatingCriticalApps.set(false);

          if (response.error) {
            this.toastService.showToast({
              variant: "error",
              title: "",
              message: this.i18nService.t("applicationsMarkedAsCriticalFail"),
            });
            return;
          }

          this.toastService.showToast({
            variant: "success",
            title: "",
            message: this.i18nService.t("numCriticalApplicationsMarkedSuccess", count),
          });
          this.selectedUrls.set(new Set<string>());
          this.criticalApplicationsCount.set(
            response?.data?.summaryData?.totalCriticalApplicationCount ?? 0,
          );
        },
        error: () => {
          this.updatingCriticalApps.set(false);
          this.toastService.showToast({
            variant: "error",
            title: "",
            message: this.i18nService.t("applicationsMarkedAsCriticalFail"),
          });
        },
      });
  }

  async unmarkAppsAsCritical() {
    this.updatingCriticalApps.set(true);
    const appsToUnmark = this.visibleSelectedApps();

    this.dataService
      .removeCriticalApplications(appsToUnmark)
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe({
        next: (response) => {
          this.updatingCriticalApps.set(false);

          if (response.error) {
            this.toastService.showToast({
              variant: "error",
              title: "",
              message: this.i18nService.t("applicationsUnmarkedAsCriticalFail"),
            });
            return;
          }

          this.toastService.showToast({
            message: this.i18nService.t(
              "numApplicationsUnmarkedCriticalSuccess",
              appsToUnmark.size,
            ),
            variant: "success",
          });
          this.selectedUrls.set(new Set<string>());
          this.criticalApplicationsCount.set(
            response?.data?.summaryData?.totalCriticalApplicationCount ?? 0,
          );
        },
        error: () => {
          this.updatingCriticalApps.set(false);
          this.toastService.showToast({
            variant: "error",
            title: "",
            message: this.i18nService.t("applicationsUnmarkedAsCriticalFail"),
          });
        },
      });
  }

  async requestPasswordChange() {
    const orgId = this.organizationId();
    if (!orgId) {
      this.toastService.showToast({
        message: this.i18nService.t("unexpectedError"),
        variant: "error",
        title: this.i18nService.t("error"),
      });
      return;
    }

    try {
      await this.securityTasksService.requestPasswordChangeForCriticalApplications(
        orgId,
        this.unassignedCipherIds(),
      );
      this.toastService.showToast({
        message: this.i18nService.t("notifiedMembers"),
        variant: "success",
        title: this.i18nService.t("success"),
      });
    } catch {
      this.toastService.showToast({
        message: this.i18nService.t("unexpectedError"),
        variant: "error",
        title: this.i18nService.t("error"),
      });
    }
  }

  readonly showAppAtRiskMembers = async (applicationName: string) => {
    await this.dataService.setDrawerForAppAtRiskMembers(applicationName);
  };

  onCheckboxChange({ applicationName, checked }: { applicationName: string; checked: boolean }) {
    this.selectedUrls.update((selectedUrls) => {
      const nextSelected = new Set(selectedUrls);
      if (checked) {
        nextSelected.add(applicationName);
      } else {
        nextSelected.delete(applicationName);
      }
      return nextSelected;
    });
  }

  onSelectAllChange(checked: boolean) {
    const filteredData = this.filteredTableData();
    if (!filteredData) {
      return;
    }

    this.selectedUrls.update((selectedUrls) => {
      const nextSelected = new Set(selectedUrls);
      filteredData.forEach((row) =>
        checked ? nextSelected.add(row.applicationName) : nextSelected.delete(row.applicationName),
      );
      return nextSelected;
    });
  }

  downloadApplicationsCSV() {
    try {
      const data = this.dataSource.filteredData;
      if (!data || data.length === 0) {
        return;
      }

      const exportData = data.map((app) => ({
        applicationName: app.applicationName,
        atRiskPasswordCount: app.atRiskPasswordCount,
        passwordCount: app.passwordCount,
        atRiskMemberCount: app.atRiskMemberCount,
        memberCount: app.memberCount,
        isMarkedAsCritical: app.isMarkedAsCritical
          ? this.i18nService.t("yes")
          : this.i18nService.t("no"),
      }));

      this.fileDownloadService.download({
        fileName: ExportHelper.getFileName("applications"),
        blobData: exportToCSV(exportData, {
          applicationName: this.i18nService.t("application"),
          atRiskPasswordCount: this.i18nService.t("atRiskPasswords"),
          passwordCount: this.i18nService.t("totalPasswords"),
          atRiskMemberCount: this.i18nService.t("atRiskMembers"),
          memberCount: this.i18nService.t("totalMembers"),
          isMarkedAsCritical: this.i18nService.t("criticalBadge"),
        }),
        blobOptions: { type: "text/plain" },
      });
    } catch (error) {
      this.logService.error("Failed to download applications CSV", error);
    }
  }
}
