import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Router} from "@angular/router";
import {AuthenticationService} from "../../modules/security/authentication.service";


@Injectable({
              providedIn: 'root'
            })
export class AuditService {

  constructor(private httpClient: HttpClient,
              private router: Router,
              private authService: AuthenticationService) {
  }

  public logActionAudit(eventType: string, fileName?: string): void {
    if (this.authService.isAuthenticated()) {
      const currentRoute = this.router.url;
      const sourceUrl = window.location.href;
      const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

      const data: AuditAction = {
        eventType: eventType,
        sourceUrl: sourceUrl,
        route: currentRoute,
        fileName: fileName
      };

      this.httpClient.post("audit/action", data, {
        headers: {
          "Client-Time-Zone": timeZone,
        }
      }).toPromise();
    }
  }
}

export interface AuditAction {
  eventType: string;
  sourceUrl: string;
  route: string;
  fileName?: string;
}

export class AuditConstants {
  public static readonly OPEN_PAGE_EVENT = "Открытие страницы";
}