import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/core';
import { OnboardingService } from './onboarding.service';
import { AnalyticsService, NavigationService, SDKService, STFUtils } from '@flaps/core';
import { Observable, of, ReplaySubject, take, tap } from 'rxjs';
import { OnboardingPayload } from './onboarding.models';
import { Account, KnowledgeBoxCreation, LearningConfigurations, WorkflowType } from '@nuclia/core';
import { LearningConfigurationForm } from './embeddings-model-form';
import { CommonModule } from '@angular/common';
import { UserContainerComponent } from '@nuclia/user';
import { TranslateModule } from '@ngx-translate/core';
import { Step1Component } from './step1/step1.component';
import { EmbeddingModelStepComponent, KbNameStepComponent, ZoneStepComponent } from './kb-creation-steps';
import { SettingUpComponent } from './setting-up/setting-up.component';
import { AccountWorkflowComponent } from './account-workflow/account-workflow.component';
import { PaButtonModule } from '@guillotinaweb/pastanaga-angular';

@Component({
  selector: 'nus-onboarding',
  templateUrl: './onboarding.component.html',
  styleUrls: ['./onboarding.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    AccountWorkflowComponent,
    CommonModule,
    UserContainerComponent,
    TranslateModule,
    Step1Component,
    KbNameStepComponent,
    SettingUpComponent,
    ZoneStepComponent,
    EmbeddingModelStepComponent,
    PaButtonModule,
  ],
})
export class OnboardingComponent {
  onboardingStep: Observable<number> = this.onboardingService.onboardingStep;
  lastStep = 6;

  onboardingInquiryPayload?: OnboardingPayload;
  kbName = '';
  zone = '';
  isCowork = false;

  learningSchemasByZone: { [zone: string]: LearningConfigurations } = {};
  learningSchema = new ReplaySubject<LearningConfigurations>(1);

  learningConfig?: LearningConfigurationForm;
  account?: Account;
  creatingAccount = false;
  inRaoApp = this.navigation.inRaoApp;
  showLogout = false;

  constructor(
    private onboardingService: OnboardingService,
    private sdk: SDKService,
    private cdr: ChangeDetectorRef,
    private analytics: AnalyticsService,
    private navigation: NavigationService,
  ) {}

  goBack(): void {
    this.onboardingService.previousStep();
  }

  createAccountAndInquiry($event: OnboardingPayload | null) {
    this.creatingAccount = true;
    this.cdr.markForCheck();
    if ($event) {
      this.onboardingInquiryPayload = $event;
      this.onboardingService.saveOnboardingInquiry(this.onboardingInquiryPayload);
    }
    this.onboardingService
      .createAccount()
      .pipe(take(1))
      .subscribe({
        next: (account) => {
          this.account = account;
          this.creatingAccount = false;
          if (this.account.workflow === 'cowork') {
            this.kbName = 'ContextBox';
            this.isCowork = true;
            this.onboardingService.switchToPreset();
          }
          this.onboardingService.nextStep();
        },
        error: () => {
          this.showLogout = true;
          this.cdr.markForCheck();
        },
      });
  }

  storeKbNameAndGoNext($event: string) {
    this.kbName = $event;
    this.onboardingService.nextStep();
  }

  storeWorkflowAndGoNext(workflow: WorkflowType) {
    if (workflow === 'cowork') {
      this.kbName = 'ContextBox';
      this.isCowork = true;
    }
    this.onboardingService.modifyAccount(this.account?.slug || '', { workflow }).subscribe(() => {
      this.onboardingService.nextStep();
    });
  }

  storeZoneAndGoNext(zone: string) {
    this.zone = zone;
    if (this.account) {
      const learningSchema$ = this.learningSchemasByZone[zone]
        ? of(this.learningSchemasByZone[zone])
        : this.sdk.nuclia.db
            .getLearningSchema(this.account.id, zone)
            .pipe(tap((schema) => (this.learningSchemasByZone[zone] = schema)));
      learningSchema$.subscribe((schema) => {
        this.learningSchema.next(schema);
        this.onboardingService.nextStep();
      });
    }
  }

  storeLearningConfigAndGoNext(config: LearningConfigurationForm) {
    this.learningConfig = config;
    this.onboardingService.nextStep();
    this.finalStepDone();
  }

  logout() {
    this.sdk.nuclia.auth.logout();
  }

  private finalStepDone() {
    if (!this.account || !this.learningConfig) {
      return;
    }

    const kbConfig: KnowledgeBoxCreation = {
      slug: STFUtils.generateSlug(this.kbName),
      title: this.kbName,
      learning_configuration: this.learningConfig,
      zone: this.zone,
      enforce_security: true,
    };

    this.onboardingService
      .createKb(this.account.slug, this.account.id, kbConfig, this.zone)
      .subscribe(() => this.analytics.logTrialActivation());
  }
}
