import { Component, inject, computed, signal, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { Store } from '@ngrx/store';
import { SidebarComponent } from '../sidebar/sidebar.component';
import { TopbarComponent } from '../topbar/topbar.component';
import { ModuleNavComponent } from '../../shared/components/module-nav/module-nav.component';
import { FeedbackButtonComponent } from '../../features/feedback/feedback-button/feedback-button.component';
import { FeedbackModalComponent } from '../../features/feedback/feedback-modal/feedback-modal.component';
import { AiAssistantComponent } from '../../shared/components/ai-assistant/ai-assistant.component';
import { selectUser } from '../../core/store';
import { ThemeService } from '../../core/services/theme.service';
import { SettingService } from '../../core/services/setting.service';

@Component({
  selector: 'app-main-layout',
  standalone: true,
  imports: [
    CommonModule,
    RouterModule,
    SidebarComponent,
    TopbarComponent,
    ModuleNavComponent,
    FeedbackButtonComponent,
    FeedbackModalComponent,
    AiAssistantComponent,
  ],
  templateUrl: './main-layout.component.html',
  styleUrl: './main-layout.component.scss',
})
export class MainLayoutComponent implements OnInit {
  private store = inject(Store);
  private settingService = inject(SettingService);
  themeService = inject(ThemeService);
  user$ = this.store.select(selectUser);

  sidebarWidth = computed(() => this.themeService.getSidebarCollapsedWidth());
  feedbackVisible = signal(false);
  showFeedbackButton = signal(true);

  ngOnInit(): void {
    this.settingService.getByKey('core.show_feedback_button').subscribe({
      next: (res: any) => {
        const val = res?.data?.value ?? res?.value;
        this.showFeedbackButton.set(val === 'true' || val === true || val === 'True' || val == null);
      },
      error: () => {},
    });
  }
}
