package com.saas.shared.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * TenantResolutionResult DTO
 * 
 * Encapsulates the result of tenant resolution for VoIP webhooks,
 * including which strategy was used to identify the tenant.
 * 
 * Resolution Strategies (in order):
 * 1. TO_NUMBER: Standard lookup by destination number (To field)
 * 2. FROM_NUMBER: Fallback to caller number (From field) if allowCallerIdFallback=true
 * 3. CUSTOM_MAPPING: Explicit caller ID mapping from caller_id_mappings table
 * 4. NOT_FOUND: No tenant could be resolved
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TenantResolutionResult {

    /**
     * The resolved tenant ID (null if not found)
     */
    private String tenantId;

    /**
     * The database schema name for the tenant (null if not found)
     */
    private String schemaName;

    /**
     * Strategy used to resolve the tenant
     */
    private ResolutionStrategy strategy;

    /**
     * The phone number that matched (could be To or From depending on strategy)
     */
    private String matchedPhoneNumber;

    /**
     * Whether tenant was successfully resolved
     */
    private boolean resolved;

    /**
     * Optional error message if resolution failed
     */
    private String errorMessage;

    public enum ResolutionStrategy {
        TO_NUMBER,         // Resolved by destination phone number (standard)
        FROM_NUMBER,       // Resolved by caller phone number (fallback)
        CUSTOM_MAPPING,    // Resolved by explicit caller ID mapping
        NOT_FOUND          // Could not resolve tenant
    }

    /**
     * Factory method for successful resolution
     */
    public static TenantResolutionResult success(String tenantId, String schemaName, 
                                                  ResolutionStrategy strategy, String matchedPhone) {
        return TenantResolutionResult.builder()
                .tenantId(tenantId)
                .schemaName(schemaName)
                .strategy(strategy)
                .matchedPhoneNumber(matchedPhone)
                .resolved(true)
                .build();
    }

    /**
     * Factory method for failed resolution
     */
    public static TenantResolutionResult notFound(String errorMessage) {
        return TenantResolutionResult.builder()
                .strategy(ResolutionStrategy.NOT_FOUND)
                .resolved(false)
                .errorMessage(errorMessage)
                .build();
    }
}
