import Foundation

// MARK: - Banner Content Type

/// What fills the banner area of the card header.
enum BannerContentType: Sendable {
    /// Profile photo fills the entire banner
    case profile
    /// Logo (3:2 landscape) fills the banner
    case logo
    /// Cover photo fills the banner
    case cover
}

// MARK: - Content Overlay Type

/// What overlaps from the banner into the content area.
enum ContentOverlayType: Sendable {
    /// No overlay - content starts immediately below banner
    case none
    /// Circular avatar overlapping from banner
    case avatar
    /// Logo rectangle (3:2) overlapping from banner
    case logoRectangle
    /// Both avatar and logo displayed side-by-side overlapping
    case avatarAndLogo
}

// MARK: - Card Header Layout

/// Defines how the business card header arranges profile, cover, and logo images.
/// These 8 layouts match the reference app exactly.
enum CardHeaderLayout: String, CaseIterable, Identifiable, Hashable, Sendable {
    
    // MARK: - Cases (8 templates matching reference app)
    
    /// 1. Profile photo fills the entire banner area, no overlay.
    /// Banner: Profile | Overlay: None
    case profileBanner
    
    /// 2. Logo (3:2 landscape) fills the entire banner area, no overlay.
    /// Banner: Logo | Overlay: None
    case logoBanner
    
    /// 3. Cover photo fills the banner, no overlay.
    /// Banner: Cover | Overlay: None
    case coverOnly
    
    /// 4. Profile photo fills banner, logo badge overlaps in content.
    /// Banner: Profile | Overlay: Logo Badge
    case profileWithLogoBadge
    
    /// 5. Logo fills the banner, avatar overlaps in content.
    /// Banner: Logo | Overlay: Avatar
    case logoWithAvatar
    
    /// 6. Cover photo fills banner, avatar overlaps in content.
    /// Banner: Cover | Overlay: Avatar
    case coverWithAvatar
    
    /// 7. Cover photo fills banner, logo rectangle overlaps in content.
    /// Banner: Cover | Overlay: Logo Rectangle
    case coverWithLogo
    
    /// 8. Cover photo fills banner, avatar and logo side-by-side overlap in content.
    /// Banner: Cover | Overlay: Avatar + Logo
    case coverWithAvatarAndLogo

    // MARK: - Identifiable
    
    var id: String { rawValue }

    // MARK: - Display Properties
    
    var displayName: String {
        switch self {
        case .profileBanner:
            return String.localized("Profile Banner")
        case .logoBanner:
            return String.localized("Logo Banner")
        case .coverOnly:
            return String.localized("Cover Only")
        case .profileWithLogoBadge:
            return String.localized("Profile + Logo")
        case .logoWithAvatar:
            return String.localized("Logo + Avatar")
        case .coverWithAvatar:
            return String.localized("Cover + Avatar")
        case .coverWithLogo:
            return String.localized("Cover + Logo")
        case .coverWithAvatarAndLogo:
            return String.localized("Cover + Both")
        }
    }
    
    var description: String {
        switch self {
        case .profileBanner:
            return String.localized("Your photo fills the banner")
        case .logoBanner:
            return String.localized("Company logo fills the banner")
        case .coverOnly:
            return String.localized("Cover image only")
        case .profileWithLogoBadge:
            return String.localized("Profile with logo badge")
        case .logoWithAvatar:
            return String.localized("Logo with avatar overlay")
        case .coverWithAvatar:
            return String.localized("Cover with avatar overlay")
        case .coverWithLogo:
            return String.localized("Cover with logo overlay")
        case .coverWithAvatarAndLogo:
            return String.localized("Cover with avatar and logo")
        }
    }
    
    /// Icon for the layout selector
    var iconName: String {
        switch self {
        case .profileBanner:
            return "person.crop.rectangle.fill"
        case .logoBanner:
            return "building.2.crop.circle.fill"
        case .coverOnly:
            return "photo.fill"
        case .profileWithLogoBadge:
            return "person.crop.square.badge.checkmark"
        case .logoWithAvatar:
            return "building.2.fill"
        case .coverWithAvatar:
            return "person.crop.rectangle.stack.fill"
        case .coverWithLogo:
            return "rectangle.stack.fill"
        case .coverWithAvatarAndLogo:
            return "rectangle.split.2x1.fill"
        }
    }
    
    // MARK: - Rendering Properties
    
    /// What fills the banner area
    var bannerContent: BannerContentType {
        switch self {
        case .profileBanner, .profileWithLogoBadge:
            return .profile
        case .logoBanner, .logoWithAvatar:
            return .logo
        case .coverOnly, .coverWithAvatar, .coverWithLogo, .coverWithAvatarAndLogo:
            return .cover
        }
    }
    
    /// What overlaps into the content area
    var contentOverlay: ContentOverlayType {
        switch self {
        case .profileBanner, .logoBanner, .coverOnly:
            return .none
        case .profileWithLogoBadge:
            return .logoRectangle
        case .logoWithAvatar, .coverWithAvatar:
            return .avatar
        case .coverWithLogo:
            return .logoRectangle
        case .coverWithAvatarAndLogo:
            return .avatarAndLogo
        }
    }
    
    /// Whether this layout has content that overlaps from banner into content area
    var hasOverlappingContent: Bool {
        contentOverlay != .none
    }
    
    // MARK: - Image Requirements
    
    /// Whether this layout requires a cover photo.
    var requiresCoverPhoto: Bool {
        bannerContent == .cover
    }
    
    /// Whether this layout requires a company logo.
    var requiresLogo: Bool {
        switch self {
        case .logoBanner, .profileWithLogoBadge, .logoWithAvatar, .coverWithLogo, .coverWithAvatarAndLogo:
            return true
        case .profileBanner, .coverOnly, .coverWithAvatar:
            return false
        }
    }
    
    /// Whether this layout requires a profile photo.
    var requiresProfilePhoto: Bool {
        switch self {
        case .profileBanner, .profileWithLogoBadge:
            return true
        case .logoBanner, .coverOnly, .logoWithAvatar, .coverWithAvatar, .coverWithLogo, .coverWithAvatarAndLogo:
            return false
        }
    }
    
    /// Returns images that are missing for this layout to render properly.
    func missingImages(hasProfile: Bool, hasCover: Bool, hasLogo: Bool) -> [String] {
        var missing: [String] = []
        
        if requiresCoverPhoto && !hasCover {
            missing.append(String.localized("Cover"))
        }
        if requiresLogo && !hasLogo {
            missing.append(String.localized("Logo"))
        }
        if requiresProfilePhoto && !hasProfile {
            missing.append(String.localized("Profile"))
        }
        
        return missing
    }
    
    /// Whether all required images are present.
    func hasAllRequiredImages(hasProfile: Bool, hasCover: Bool, hasLogo: Bool) -> Bool {
        missingImages(hasProfile: hasProfile, hasCover: hasCover, hasLogo: hasLogo).isEmpty
    }
    
    // MARK: - Suggestion Logic
    
    /// Returns the best layout based on available images.
    nonisolated static func suggested(hasProfile: Bool, hasCover: Bool, hasLogo: Bool) -> CardHeaderLayout {
        switch (hasCover, hasLogo, hasProfile) {
        case (true, true, true):
            return .coverWithAvatarAndLogo
        case (true, true, false):
            return .coverWithLogo
        case (true, false, true):
            return .coverWithAvatar
        case (true, false, false):
            return .coverOnly
        case (false, true, true):
            return .logoWithAvatar
        case (false, true, false):
            return .logoBanner
        case (false, false, true):
            return .profileBanner
        case (false, false, false):
            return .profileBanner
        }
    }
}
