import logging

logger = logging.getLogger(__name__)


def generate_listing_copy(domain: str, brand_name: str, tagline: str, niche: str,
                          quality_score: int, quality_tier: str, listing_price: int,
                          feature_count: int, augment_count: int, section_count: int,
                          has_pricing: bool, has_faq: bool, has_testimonials: bool,
                          headline: str = "", subheadline: str = "") -> dict:
    tier_label = quality_tier.title()
    agency_value = listing_price * 8

    title = f"{brand_name} - Premium {niche} Website Package | {domain} | Ready to Deploy"
    if len(title) > 80:
        title = f"{brand_name} - {niche} Website | {domain} | Deploy Ready"

    sections_list = []
    if feature_count > 0: sections_list.append(f"{feature_count} feature cards")
    if has_pricing: sections_list.append("pricing tiers")
    if has_faq: sections_list.append("FAQ section")
    if has_testimonials: sections_list.append("testimonials")
    if augment_count > 0: sections_list.append(f"{augment_count} interactive tools/calculators")
    sections_str = ", ".join(sections_list) if sections_list else "comprehensive content sections"

    description = f"""# {brand_name} — {tier_label} Website Package

**Domain:** {domain}
**Niche:** {niche}
**Quality Score:** {quality_score}/100 ({tier_label} Tier)
**Comparable Agency Value:** ${agency_value:,}+

---

## What You Get

This is a complete, deployment-ready website package built with AI-powered content generation and professional brand design. Everything you need to launch a professional online presence — immediately.

### Included in This Package:

- **Complete Website Source Code** — Clean HTML/CSS/JS, mobile-responsive, production-ready
- **Brand Identity** — Professional logo concepts, color palette, typography system, and design tokens
- **Hero Image** — Custom AI-generated hero banner designed for this specific brand
- **Full Content Package** — {sections_str}
- **{section_count}+ Content Sections** — Each professionally written with conversion-focused copy
- **SEO-Optimized** — Meta tags, structured data, and semantic HTML built in

{f'### Interactive Tools & Calculators' + chr(10) + f'This package includes {augment_count} custom-built interactive tools that increase visitor engagement and time-on-site.' + chr(10) if augment_count > 0 else ''}
### Why This Package?

{headline if headline else f'Professional {niche} website ready for immediate deployment.'}
{subheadline if subheadline else ''}

Every element has been generated by our AI pipeline and quality-checked by our 12-point scoring engine. The {quality_score}/100 score means this package meets our {tier_label.lower()} quality threshold across all dimensions.

---

## Deployment

Upload to any web hosting provider (shared hosting, VPS, cloud). The site works out of the box — no framework dependencies, no build steps required. Just upload and go.

## Perfect For

- Entrepreneurs entering the {niche.lower()} space
- Web agencies looking for ready-made client deliverables
- Domain investors wanting to maximize domain value
- Anyone who needs a professional website without the $5,000+ agency price tag

---

**Buy with confidence.** Quality score visible, preview available, and all source code included.
"""

    bullets = [
        f"Complete {niche} website — production-ready source code",
        f"Quality Score: {quality_score}/100 ({tier_label} tier)",
        f"Brand identity: logo, colors, typography, design system",
        "Custom AI-generated hero image",
        f"{section_count}+ content sections with conversion-focused copy",
    ]
    if augment_count > 0:
        bullets.append(f"{augment_count} interactive tools/calculators included")
    bullets.extend([
        "Mobile-responsive, SEO-optimized, deploy-ready",
        f"Comparable agency value: ${agency_value:,}+",
    ])

    flippa_title = f"{domain} | {brand_name} - {tier_label} {niche} Website Package (Score: {quality_score}/100)"
    if len(flippa_title) > 100:
        flippa_title = f"{domain} | {brand_name} - {niche} Website ({quality_score}/100)"

    ebay_title = f"Premium {niche} Website + Domain: {domain} | Ready to Deploy | Score {quality_score}/100"
    if len(ebay_title) > 80:
        ebay_title = f"{niche} Website + Domain: {domain} | Deploy Ready"

    return {
        "title": title,
        "flippa_title": flippa_title,
        "ebay_title": ebay_title,
        "description": description.strip(),
        "bullets": bullets,
        "price": listing_price,
        "agency_value": agency_value,
        "quality_score": quality_score,
        "quality_tier": quality_tier,
    }
