from pathlib import Path
from textwrap import wrap

from reportlab.lib.colors import HexColor
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas

ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "output" / "pdf" / "logo-redesign-scorecard.pdf"
PUBLIC_OUTPUT = (
    ROOT
    / "public"
    / "assets"
    / "downloads"
    / "brandsquare-logo-redesign-scorecard.pdf"
)

WIDTH, HEIGHT = A4
INK = HexColor("#142522")
ORANGE = HexColor("#ff6846")
LIME = HexColor("#caff62")
WHITE = HexColor("#ffffff")
MUTED = HexColor("#62706b")
LINE = HexColor("#d9ded6")
PALE = HexColor("#f5f7f2")
ROSE = HexColor("#623747")


def wrapped(
    pdf,
    value,
    x,
    y,
    width,
    size=10,
    leading=15,
    color=MUTED,
    font="Helvetica",
):
    pdf.setFont(font, size)
    pdf.setFillColor(color)
    max_chars = max(12, int(width / (size * 0.52)))
    lines = []
    for paragraph in value.split("\n"):
        lines.extend(wrap(paragraph, max_chars) or [""])
    for line in lines:
        pdf.drawString(x, y, line)
        y -= leading
    return y


def brand(pdf, x=46, y=HEIGHT - 48, light=False):
    pdf.setFont("Helvetica-Bold", 20)
    pdf.setFillColor(WHITE if light else INK)
    pdf.drawString(x, y, "Brand")
    pdf.setFillColor(LIME if light else ORANGE)
    pdf.drawString(x + 55, y, "Square.")


def footer(pdf, page):
    pdf.setStrokeColor(LINE)
    pdf.line(46, 38, WIDTH - 46, 38)
    pdf.setFont("Helvetica", 7)
    pdf.setFillColor(MUTED)
    pdf.drawString(46, 24, "BrandSquare - Logo Redesign Scorecard")
    pdf.drawRightString(WIDTH - 46, 24, f"{page:02d} / 07")


def page_header(pdf, number, label, title, intro):
    brand(pdf)
    pdf.setFillColor(ORANGE)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(46, HEIGHT - 98, f"{number:02d}  /  {label.upper()}")
    pdf.setFillColor(INK)
    pdf.setFont("Helvetica-Bold", 28)
    pdf.drawString(46, HEIGHT - 137, title)
    wrapped(pdf, intro, 46, HEIGHT - 164, WIDTH - 92, 10, 15)


def cover(pdf):
    pdf.setFillColor(INK)
    pdf.rect(0, 0, WIDTH, HEIGHT, fill=1, stroke=0)
    brand(pdf, light=True)
    pdf.setFillColor(ORANGE)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(46, HEIGHT - 115, "FREE BRAND AUDIT WORKBOOK  /  INDIA 2026")
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 39)
    pdf.drawString(46, HEIGHT - 187, "LOGO REDESIGN")
    pdf.setFillColor(LIME)
    pdf.drawString(46, HEIGHT - 233, "SCORECARD")
    wrapped(
        pdf,
        "A practical 100-point audit to decide whether your logo should be retained, refreshed, redesigned, or rebuilt.",
        46,
        HEIGHT - 278,
        420,
        13,
        20,
        HexColor("#c7d2ce"),
    )
    pdf.setFillColor(ROSE)
    pdf.roundRect(46, 162, WIDTH - 92, 208, 8, fill=1, stroke=0)
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 16)
    pdf.drawString(72, 330, "Your seven-minute logo audit")
    items = [
        ("01", "Recognition and memory"),
        ("02", "Clarity and flexibility"),
        ("03", "Relevance and differentiation"),
        ("04", "Trust and ownership"),
    ]
    y = 294
    for number, item in items:
        pdf.setFillColor(LIME)
        pdf.setFont("Helvetica-Bold", 9)
        pdf.drawString(72, y, number)
        pdf.setFillColor(WHITE)
        pdf.setFont("Helvetica", 11)
        pdf.drawString(108, y, item)
        y -= 33
    pdf.setFillColor(ORANGE)
    pdf.roundRect(WIDTH - 178, 71, 132, 50, 4, fill=1, stroke=0)
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 9)
    pdf.drawCentredString(WIDTH - 112, 99, "TOTAL SCORE")
    pdf.setFont("Helvetica-Bold", 16)
    pdf.drawCentredString(WIDTH - 112, 80, "____ / 100")
    pdf.setFont("Helvetica", 8)
    pdf.setFillColor(HexColor("#8fa09a"))
    pdf.drawString(
        46,
        82,
        "Prepared by BrandSquare - Strategic logo redesign for ambitious businesses.",
    )
    pdf.showPage()


def how_to_score(pdf):
    page_header(
        pdf,
        2,
        "Before you begin",
        "How to score your current logo",
        "Judge the logo you use today, not the version you hope to create. Circle one score for every statement and add each category subtotal.",
    )
    levels = [
        ("1", "Strongly disagree", "A clear warning sign"),
        ("2", "Mostly disagree", "Needs meaningful work"),
        ("3", "Unsure or mixed", "Inconsistent performance"),
        ("4", "Mostly agree", "Working with minor gaps"),
        ("5", "Strongly agree", "A proven brand strength"),
    ]
    y = 603
    for number, title, note in levels:
        pdf.setFillColor(PALE)
        pdf.roundRect(46, y - 58, WIDTH - 92, 48, 5, fill=1, stroke=0)
        pdf.setFillColor(ORANGE)
        pdf.circle(72, y - 34, 13, fill=1, stroke=0)
        pdf.setFillColor(WHITE)
        pdf.setFont("Helvetica-Bold", 11)
        pdf.drawCentredString(72, y - 38, number)
        pdf.setFillColor(INK)
        pdf.setFont("Helvetica-Bold", 11)
        pdf.drawString(98, y - 29, title)
        pdf.setFillColor(MUTED)
        pdf.setFont("Helvetica", 8.5)
        pdf.drawString(98, y - 43, note)
        y -= 65
    pdf.setFillColor(INK)
    pdf.roundRect(46, 105, WIDTH - 92, 116, 6, fill=1, stroke=0)
    pdf.setFillColor(LIME)
    pdf.setFont("Helvetica-Bold", 9)
    pdf.drawString(68, 190, "AUDIT RULE")
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 17)
    pdf.drawString(68, 161, "Score what customers experience.")
    wrapped(
        pdf,
        "Ask a customer or colleague to complete a second copy. A large difference between your scores is useful evidence that the logo is not communicating consistently.",
        68,
        137,
        WIDTH - 136,
        8.8,
        13,
        HexColor("#c6d1cd"),
    )
    footer(pdf, 2)
    pdf.showPage()


def score_row(pdf, y, number, statement):
    pdf.setFillColor(WHITE)
    pdf.roundRect(46, y - 72, WIDTH - 92, 62, 5, fill=1, stroke=0)
    pdf.setFillColor(ORANGE)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(62, y - 34, f"{number:02d}")
    wrapped(pdf, statement, 91, y - 29, 305, 9.2, 13, INK, "Helvetica-Bold")
    x = 423
    for score in range(1, 6):
        pdf.setStrokeColor(ORANGE)
        pdf.circle(x, y - 41, 10, fill=0, stroke=1)
        pdf.setFillColor(MUTED)
        pdf.setFont("Helvetica-Bold", 7)
        pdf.drawCentredString(x, y - 44, str(score))
        x += 25


def category_page(pdf, page, title, intro, statements, prompt):
    page_header(pdf, page, "Score the evidence", title, intro)
    y = 612
    for index, statement in enumerate(statements, 1):
        score_row(pdf, y, index, statement)
        y -= 82
    pdf.setFillColor(INK)
    pdf.roundRect(46, 96, WIDTH - 92, 84, 6, fill=1, stroke=0)
    pdf.setFillColor(LIME)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(65, 151, prompt.upper())
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 16)
    pdf.drawString(65, 121, "CATEGORY SUBTOTAL")
    pdf.drawRightString(WIDTH - 65, 121, "______ / 25")
    footer(pdf, page)
    pdf.showPage()


def results(pdf):
    page_header(
        pdf,
        7,
        "Read the result",
        "What your total score means",
        "Add the four category subtotals. Use the score as a decision guide, then consider the red flags below before choosing the scope of change.",
    )
    bands = [
        ("80-100", "RETAIN + REFINE", "Protect recognition. Correct only specific usability or consistency gaps.", LIME, INK),
        ("60-79", "STRATEGIC REFRESH", "Keep recognisable equity while simplifying and modernising the system.", HexColor("#ffe19a"), INK),
        ("40-59", "REDESIGN", "The logo is limiting trust or growth. Revisit the idea, structure, and execution.", ORANGE, WHITE),
        ("0-39", "REBUILD", "Start from business strategy. Very little existing visual equity is working.", ROSE, WHITE),
    ]
    y = 610
    for score, action, explanation, fill, text in bands:
        pdf.setFillColor(fill)
        pdf.roundRect(46, y - 87, WIDTH - 92, 76, 6, fill=1, stroke=0)
        pdf.setFillColor(text)
        pdf.setFont("Helvetica-Bold", 17)
        pdf.drawString(64, y - 43, score)
        pdf.setFont("Helvetica-Bold", 10)
        pdf.drawString(143, y - 33, action)
        wrapped(pdf, explanation, 143, y - 49, 375, 8.3, 11, text)
        y -= 83
    pdf.setFillColor(PALE)
    pdf.roundRect(46, 175, WIDTH - 92, 93, 6, fill=1, stroke=0)
    pdf.setFillColor(ORANGE)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(63, 243, "OVERRIDE RED FLAGS")
    wrapped(
        pdf,
        "Unreadable at small sizes  /  Similar to a competitor  /  Missing vector files  /  Ownership is unclear  /  Name or positioning has changed",
        63,
        222,
        WIDTH - 126,
        8.5,
        13,
        INK,
        "Helvetica-Bold",
    )
    pdf.setFillColor(INK)
    pdf.roundRect(46, 67, WIDTH - 92, 88, 6, fill=1, stroke=0)
    pdf.setFillColor(LIME)
    pdf.setFont("Helvetica-Bold", 8)
    pdf.drawString(64, 132, "WANT A PROFESSIONAL SECOND OPINION?")
    pdf.setFillColor(WHITE)
    pdf.setFont("Helvetica-Bold", 17)
    pdf.drawString(64, 104, "Request a BrandSquare logo audit.")
    pdf.setFillColor(ORANGE)
    pdf.setFont("Helvetica-Bold", 9)
    pdf.drawString(64, 82, "brandsquare.co.in  /  hello@brandsquare.co.in")
    footer(pdf, 7)
    pdf.showPage()


def build():
    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    PUBLIC_OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    pdf = canvas.Canvas(str(OUTPUT), pagesize=A4, pageCompression=1)
    pdf.setTitle("Logo Redesign Scorecard - BrandSquare")
    pdf.setAuthor("BrandSquare")
    cover(pdf)
    how_to_score(pdf)
    category_page(
        pdf,
        3,
        "Recognition and memory",
        "A redesign should protect genuine brand equity. Score whether customers can identify and recall the logo without extra explanation.",
        [
            "Customers recognise the logo without needing to read the full business name.",
            "The logo is easy to remember after a brief first impression.",
            "Existing customers would notice if the logo changed significantly.",
            "The logo contains at least one distinctive visual feature worth protecting.",
            "The logo is used consistently enough to have built recognition.",
        ],
        "Protect what people already know",
    )
    category_page(
        pdf,
        4,
        "Clarity and flexibility",
        "A modern logo must work across small digital screens, print, signage, video, products, and monochrome applications.",
        [
            "The logo remains clear and readable at social-profile and favicon sizes.",
            "It works in one colour without gradients, shadows, or special effects.",
            "Horizontal, stacked, and icon-only versions are available when needed.",
            "The logo reproduces reliably in print, signage, embroidery, and digital use.",
            "Editable vector master files and organised exports are available.",
        ],
        "Test the logo in the real world",
    )
    category_page(
        pdf,
        5,
        "Relevance and differentiation",
        "The logo should feel appropriate for the business today while avoiding the generic visual language used by competitors.",
        [
            "The logo reflects the business you are now, not the business you used to be.",
            "Its personality fits the customers and price position you want to attract.",
            "It looks meaningfully different from direct competitors.",
            "The design avoids overused symbols, templates, and short-lived trends.",
            "The logo can support future products, locations, or services.",
        ],
        "Design for the next stage",
    )
    category_page(
        pdf,
        6,
        "Trust and ownership",
        "A credible identity should feel professionally resolved and be safe for the business to use, reproduce, and grow.",
        [
            "The logo looks intentional and professionally made rather than improvised.",
            "Typography, spacing, colour, and alignment are consistent.",
            "The logo creates the right level of trust for the price and category.",
            "The business owns the approved artwork and can prove where it came from.",
            "No obvious competitor similarity or trademark concern has been identified.",
        ],
        "Protect credibility and control",
    )
    results(pdf)
    pdf.save()
    PUBLIC_OUTPUT.write_bytes(OUTPUT.read_bytes())
    print(OUTPUT)
    print(PUBLIC_OUTPUT)


if __name__ == "__main__":
    build()
