import { Injectable, signal } from '@angular/core';
import { Observable, of } from 'rxjs';
import { ExternalIdentity, ExternalIdentityProvider } from '../models/customer-identity.model';
import { SocialIdentityGateway, SocialProvider } from './social-identity-gateway.interface';

/**
 * Development stand-in. No VK or Yandex OAuth application is registered yet,
 * and registering one is blocked on a decision that has to be made before any
 * of this can work for real: both providers validate redirect_uri against an
 * exact registered list, so a multi-tenant platform needs one central
 * identity host as the sole registered callback, with the tenant carried in
 * the signed state. See FORK-HARVEST-TODO.md FH-0.1.
 *
 * Returning a data: URL rather than a fake provider URL is deliberate - it
 * cannot be mistaken for a working flow if this ever runs outside dev.
 */
@Injectable({ providedIn: 'root' })
export class SocialIdentityLocalGateway implements SocialIdentityGateway {
  // Seeded so the account-identities screen has something to render in dev.
  // Telegram is present because in the current app it is the only real login;
  // FH-4.6 makes it one ExternalIdentity among several, which is exactly what
  // this list is meant to show.
  private readonly linked = signal<ExternalIdentity[]>([
    {
      customerId: 'customer_local',
      provider: 'telegram',
      providerUserId: '100200300',
      displayName: 'Local Telegram User',
      verifiedAt: '2026-08-01T10:00:00.000Z',
      lastUsedAt: '2026-08-21T09:00:00.000Z',
    },
  ]);

  getAuthorizeUrl(provider: SocialProvider): Observable<string> {
    return of(`about:blank#${provider}-oauth-not-configured`);
  }

  listIdentities(): Observable<ExternalIdentity[]> {
    return of(this.linked());
  }

  unlink(provider: ExternalIdentityProvider): Observable<void> {
    this.linked.update(list => list.filter(identity => identity.provider !== provider));
    return of(void 0);
  }
}
