class AppEnvironment {
  const AppEnvironment({
    required this.ssoBaseUrl,
    required this.apiBaseUrl,
    required this.authApiBaseUrl,
    required this.directoryApiBaseUrl,
    required this.organizationApiBaseUrl,
    required this.orgContextApiBaseUrl,
    required this.orgContextTenantSlug,
    required this.appVersion,
    required this.buildTimestamp,
  });

  factory AppEnvironment.fromDartDefine() {
    const fallbackBaseUrl = 'https://sso.example.invalid';
    const ssoBaseUrl = String.fromEnvironment(
      'SSO_BASE_URL',
      defaultValue: 'https://sso.hmac.kr',
    );
    const sharedApiBaseUrl = String.fromEnvironment(
      'TDC114_API_BASE',
      defaultValue: fallbackBaseUrl,
    );
    const authApiBaseOverride = String.fromEnvironment(
      'TDC114_AUTH_API_BASE',
      defaultValue: '',
    );
    const directoryApiBaseOverride = String.fromEnvironment(
      'TDC114_DIRECTORY_API_BASE',
      defaultValue: '',
    );
    const organizationApiBaseOverride = String.fromEnvironment(
      'TDC114_ORGANIZATION_API_BASE',
      defaultValue: '',
    );
    const orgContextApiBaseOverride = String.fromEnvironment(
      'TDC114_ORG_CONTEXT_API_BASE',
      defaultValue: '',
    );
    const orgContextTenantSlug = String.fromEnvironment(
      'TDC114_ORG_CONTEXT_TENANT_SLUG',
      defaultValue: 'hanmac-family',
    );
    return AppEnvironment(
      ssoBaseUrl: ssoBaseUrl,
      apiBaseUrl: sharedApiBaseUrl,
      authApiBaseUrl: authApiBaseOverride.isEmpty
          ? sharedApiBaseUrl
          : authApiBaseOverride,
      directoryApiBaseUrl: directoryApiBaseOverride.isEmpty
          ? sharedApiBaseUrl
          : directoryApiBaseOverride,
      organizationApiBaseUrl: organizationApiBaseOverride.isEmpty
          ? sharedApiBaseUrl
          : organizationApiBaseOverride,
      orgContextApiBaseUrl: orgContextApiBaseOverride.isEmpty
          ? (organizationApiBaseOverride.isEmpty
                ? sharedApiBaseUrl
                : organizationApiBaseOverride)
          : orgContextApiBaseOverride,
      orgContextTenantSlug: orgContextTenantSlug,
      appVersion: const String.fromEnvironment(
        'APP_VERSION',
        defaultValue: '0.1.0',
      ),
      buildTimestamp: const String.fromEnvironment(
        'TDC114_BUILD_TIME',
        defaultValue: '',
      ),
    );
  }

  final String ssoBaseUrl;
  final String apiBaseUrl;
  final String authApiBaseUrl;
  final String directoryApiBaseUrl;
  final String organizationApiBaseUrl;
  final String orgContextApiBaseUrl;
  final String orgContextTenantSlug;
  final String appVersion;
  final String buildTimestamp;
}
