import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { Account } from '../../models/account.model';
import { AccountDTO } from '../../models/account.dto';

@Injectable({
  providedIn: 'root'
})
export class VaultService {
  private _accounts = new BehaviorSubject<Account[]>([]);
  public accounts$: Observable<Account[]> = this._accounts.asObservable();

  constructor() {
    // Initial mock data to simulate DB
    this.loadMockData();
  }

  /**
   * Clears the current session data when the system is locked.
   */
  clear(): void {
    this._accounts.next([]);
  }

  public getAccountsSnapshot(): Account[] {
    return this._accounts.value;
  }

  // Helper mock methods for UI dev (normally IPC commands)
  private loadMockData(): void {
    const mockDTOs: AccountDTO[] = [
      {
        id: '1',
        service_name: 'Google',
        encrypted_data: 'enc_123',
        nonce: 'n_123',
        updated_at: new Date().toISOString(),
        is_deleted: false
      },
      {
        id: '2',
        service_name: 'GitHub',
        encrypted_data: 'enc_456',
        nonce: 'n_456',
        updated_at: new Date(Date.now() - 86400000).toISOString(),
        is_deleted: false
      }
    ];

    const mockAccounts = mockDTOs.map(dto => {
      const account = new Account(dto, {
        login: `user@${dto.service_name.toLowerCase()}.com`,
        password: 'mock_password_123',
        notes: 'Mock data for UI dev'
      });
      return account;
    });

    this._accounts.next(mockAccounts);
  }
}
