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-launch-kit.pdf"
PUBLIC_OUTPUT = (
    ROOT / "public" / "assets" / "downloads" / "brandsquare-logo-launch-kit.pdf"
)

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


def wrapped(c, value, x, y, width, size=10, leading=15, color=MUTED, font="Helvetica"):
    c.setFont(font, size)
    c.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:
        c.drawString(x, y, line)
        y -= leading
    return y


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


def footer(c, page):
    c.setStrokeColor(LINE)
    c.line(46, 38, WIDTH - 46, 38)
    c.setFont("Helvetica", 7)
    c.setFillColor(MUTED)
    c.drawString(46, 24, "BrandSquare - The Logo Launch Kit")
    c.drawRightString(WIDTH - 46, 24, f"{page:02d} / 08")


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


def card(c, x, y, w, h, number, title, body, fill=WHITE):
    c.setFillColor(fill)
    c.roundRect(x, y - h, w, h, 5, fill=1, stroke=0)
    c.setFillColor(ORANGE)
    c.setFont("Helvetica-Bold", 8)
    c.drawString(x + 16, y - 22, number)
    c.setFillColor(INK)
    c.setFont("Helvetica-Bold", 12)
    c.drawString(x + 16, y - 44, title)
    wrapped(c, body, x + 16, y - 62, w - 32, 8.5, 12)


def cover(c):
    c.setFillColor(INK)
    c.rect(0, 0, WIDTH, HEIGHT, fill=1, stroke=0)
    brand(c, light=True)
    c.setFillColor(ORANGE)
    c.setFont("Helvetica-Bold", 8)
    c.drawString(46, HEIGHT - 115, "FREE BRAND PLANNING FIELD GUIDE  /  INDIA 2026")
    c.setFillColor(WHITE)
    c.setFont("Helvetica-Bold", 43)
    c.drawString(46, HEIGHT - 190, "THE LOGO")
    c.setFillColor(LIME)
    c.drawString(46, HEIGHT - 238, "LAUNCH KIT")
    wrapped(
        c,
        "A practical guide to planning, pricing, and commissioning a logo that helps your business look credible from day one.",
        46,
        HEIGHT - 286,
        390,
        13,
        20,
        HexColor("#c7d2ce"),
    )
    c.setFillColor(ROSE)
    c.roundRect(46, 155, WIDTH - 92, 215, 8, fill=1, stroke=0)
    c.setFillColor(WHITE)
    c.setFont("Helvetica-Bold", 16)
    c.drawString(72, 330, "Inside this kit")
    items = [
        "The seven expensive logo mistakes to avoid",
        "A clear logo package and pricing guide",
        "The questions to ask before hiring a designer",
        "A one-page project brief worksheet",
        "The file formats every business should receive",
    ]
    y = 298
    for item in items:
        c.setFillColor(LIME)
        c.circle(76, y + 3, 3, fill=1, stroke=0)
        c.setFillColor(WHITE)
        c.setFont("Helvetica", 10)
        c.drawString(90, y, item)
        y -= 31
    c.setFont("Helvetica", 8)
    c.setFillColor(HexColor("#8fa09a"))
    c.drawString(46, 75, "Prepared by BrandSquare - Strategic logo identities for ambitious businesses.")
    c.showPage()


def page_two(c):
    page_header(
        c,
        2,
        "Start with strategy",
        "What a strong logo must do",
        "A useful logo is not decoration. It is a compact business tool that helps the right customer recognise, remember, and trust you.",
    )
    card(c, 46, 590, 238, 142, "01", "Identify", "Make it clear who the business is and create a recognisable visual signature.")
    card(c, 311, 590, 238, 142, "02", "Differentiate", "Give customers a reason to remember you instead of confusing you with competitors.")
    card(c, 46, 420, 238, 142, "03", "Reassure", "Signal professionalism and reduce doubt before a customer contacts or buys from you.")
    card(c, 311, 420, 238, 142, "04", "Scale", "Stay clear on a phone screen, invoice, signboard, product, uniform, and social profile.")
    c.setFillColor(INK)
    c.roundRect(46, 105, WIDTH - 92, 116, 5, fill=1, stroke=0)
    c.setFillColor(LIME)
    c.setFont("Helvetica-Bold", 9)
    c.drawString(66, 191, "THE SIMPLE TEST")
    c.setFillColor(WHITE)
    c.setFont("Helvetica-Bold", 17)
    c.drawString(66, 160, "Can someone recognise it in two seconds?")
    wrapped(c, "If the logo only works after an explanation, it is not doing enough work for the business.", 66, 136, WIDTH - 132, 9, 13, HexColor("#c5d0cc"))
    footer(c, 2)
    c.showPage()


def mistakes_page(c, page, start, items):
    page_header(
        c,
        page,
        "Avoid expensive rework",
        "Seven logo mistakes to avoid",
        "These shortcuts often look inexpensive at first, but cost more when the business needs printing, signage, advertising, or consistent growth.",
    )
    y = 600
    for offset, (title, body) in enumerate(items):
        number = f"{start + offset:02d}"
        c.setFillColor(WHITE)
        c.roundRect(46, y - 112, WIDTH - 92, 100, 5, fill=1, stroke=0)
        c.setFillColor(ORANGE)
        c.setFont("Helvetica-Bold", 10)
        c.drawString(63, y - 39, number)
        c.setFillColor(INK)
        c.setFont("Helvetica-Bold", 12)
        c.drawString(100, y - 39, title)
        wrapped(c, body, 100, y - 58, WIDTH - 165, 8.5, 12)
        y -= 128
    footer(c, page)
    c.showPage()


def pricing(c):
    page_header(
        c,
        5,
        "Plan the investment",
        "Logo package and pricing guide",
        "Choose the smallest package that gives the business everything it will genuinely use. BrandSquare logo projects begin at INR 7,999.",
    )
    packages = [
        ("ESSENTIALS", "From INR 7,999", "For a new or small business that needs a professional primary logo.", ["Discovery brief", "Three original concepts", "Guided refinement", "Final print and web files"]),
        ("BRAND STARTER", "Custom quotation", "For a business that needs a consistent visual foundation beyond the logo.", ["Everything in Essentials", "Colour palette", "Typography direction", "Social profile assets"]),
        ("IDENTITY SYSTEM", "Custom quotation", "For a growing brand with several customer touchpoints and teams.", ["Complete logo system", "Brand guidelines", "Launch applications", "Usage and rollout support"]),
    ]
    y = 615
    for index, (name, price, audience, items) in enumerate(packages):
        fill = INK if index == 1 else WHITE
        text = WHITE if index == 1 else INK
        c.setFillColor(fill)
        c.roundRect(46, y - 154, WIDTH - 92, 140, 5, fill=1, stroke=0)
        c.setFillColor(LIME if index == 1 else ORANGE)
        c.setFont("Helvetica-Bold", 8)
        c.drawString(63, y - 39, name)
        c.setFillColor(text)
        c.setFont("Helvetica-Bold", 15)
        c.drawString(63, y - 65, price)
        wrapped(c, audience, 63, y - 86, 205, 8, 11, HexColor("#c3d0cb") if index == 1 else MUTED)
        list_y = y - 39
        for item in items:
            c.setFillColor(LIME if index == 1 else ORANGE)
            c.circle(315, list_y + 2, 2.5, fill=1, stroke=0)
            c.setFillColor(text)
            c.setFont("Helvetica", 8.5)
            c.drawString(326, list_y - 1, item)
            list_y -= 23
        y -= 166
    c.setFillColor(HexColor("#fff1eb"))
    c.roundRect(46, 70, WIDTH - 92, 66, 5, fill=1, stroke=0)
    wrapped(c, "Ask for a written scope before paying. It should state concepts, revision process, deliverables, timeline, ownership, and exclusions.", 63, 111, WIDTH - 126, 8.5, 12, INK)
    footer(c, 5)
    c.showPage()


def hiring(c):
    page_header(
        c,
        6,
        "Choose carefully",
        "Designer hiring checklist",
        "Use these questions before choosing a freelancer, studio, or agency. Clear answers reduce misunderstandings and protect the project.",
    )
    questions = [
        "Will the concepts be created specifically for my business?",
        "How do you learn about my audience and competitors?",
        "How many genuinely different directions will I see?",
        "What does the refinement and feedback process include?",
        "Which final file formats and logo variations will I receive?",
        "Will I receive full ownership of the approved final logo?",
        "Who checks originality and possible trademark conflicts?",
        "What is the timeline and what could delay delivery?",
        "Which costs are included and which are additional?",
        "Can you show work with a clear business rationale?",
    ]
    y = 610
    for index, question in enumerate(questions):
        row = index % 5
        col = index // 5
        x = 46 + col * 265
        yy = y - row * 92
        c.setFillColor(WHITE)
        c.roundRect(x, yy - 72, 238, 64, 4, fill=1, stroke=0)
        c.setStrokeColor(ORANGE)
        c.rect(x + 14, yy - 42, 13, 13, fill=0, stroke=1)
        wrapped(c, question, x + 39, yy - 27, 180, 8.3, 11, INK)
    footer(c, 6)
    c.showPage()


def worksheet(c):
    page_header(
        c,
        7,
        "Prepare the brief",
        "One-page logo project worksheet",
        "Write short, honest answers. Good design decisions become easier when the business problem is clear.",
    )
    prompts = [
        "Business name and tagline",
        "What do you sell, and to whom?",
        "What should customers feel after seeing the brand?",
        "Name three competitors or alternatives",
        "What makes the business meaningfully different?",
        "Where will the logo be used most often?",
        "Three words the identity should communicate",
        "Colours, symbols, or styles to avoid",
    ]
    y = 610
    for index, prompt in enumerate(prompts):
        c.setFillColor(ORANGE)
        c.setFont("Helvetica-Bold", 8)
        c.drawString(46, y, f"{index + 1:02d}")
        c.setFillColor(INK)
        c.setFont("Helvetica-Bold", 9)
        c.drawString(76, y, prompt)
        c.setStrokeColor(LINE)
        c.line(76, y - 22, WIDTH - 46, y - 22)
        c.line(76, y - 42, WIDTH - 46, y - 42)
        y -= 67
    footer(c, 7)
    c.showPage()


def files_and_cta(c):
    page_header(
        c,
        8,
        "Launch correctly",
        "Files every business should receive",
        "Keep an organised master folder. Use vector files for production and raster files for everyday digital use.",
    )
    files = [
        ("SVG", "Best for websites, apps, large signs, and future editing."),
        ("PDF", "Reliable vector format for printers, vendors, and documents."),
        ("PNG", "Transparent-background files for presentations, social, and web."),
        ("JPG", "Convenient image format for simple digital and office use."),
    ]
    x = 46
    for extension, description in files:
        c.setFillColor(WHITE)
        c.roundRect(x, 485, 116, 125, 5, fill=1, stroke=0)
        c.setFillColor(ORANGE)
        c.setFont("Helvetica-Bold", 18)
        c.drawString(x + 15, 575, extension)
        wrapped(c, description, x + 15, 550, 86, 7.7, 10)
        x += 129
    c.setFillColor(INK)
    c.roundRect(46, 110, WIDTH - 92, 300, 8, fill=1, stroke=0)
    c.setFillColor(LIME)
    c.setFont("Helvetica-Bold", 9)
    c.drawString(70, 370, "READY TO BUILD A LOGO PEOPLE REMEMBER?")
    c.setFillColor(WHITE)
    c.setFont("Helvetica-Bold", 27)
    c.drawString(70, 325, "Start with a clear conversation.")
    wrapped(c, "Share your business, audience, and ambition. BrandSquare will recommend the right project scope and reply with clear next steps.", 70, 292, 410, 10, 15, HexColor("#c2cfca"))
    c.setFillColor(ORANGE)
    c.roundRect(70, 205, 230, 48, 3, fill=1, stroke=0)
    c.setFillColor(WHITE)
    c.setFont("Helvetica-Bold", 10)
    c.drawString(88, 224, "brandsquare.co.in  /  Start your brief")
    c.setFont("Helvetica", 9)
    c.setFillColor(HexColor("#c2cfca"))
    c.drawString(70, 168, "Email: hello@brandsquare.co.in")
    c.drawString(70, 150, "WhatsApp: Use the number that sent you this kit")
    footer(c, 8)
    c.showPage()


def build():
    OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    PUBLIC_OUTPUT.parent.mkdir(parents=True, exist_ok=True)
    c = canvas.Canvas(str(OUTPUT), pagesize=A4, pageCompression=1)
    c.setTitle("The Logo Launch Kit - BrandSquare")
    c.setAuthor("BrandSquare")
    cover(c)
    page_two(c)
    mistakes_page(c, 3, 1, [
        ("Starting with a symbol instead of a strategy", "A shape cannot solve an unclear positioning problem. Define the audience, promise, and personality first."),
        ("Choosing a template or copied idea", "Generic marks make the business forgettable and can create ownership or trademark problems later."),
        ("Adding too much detail", "Complex logos become unreadable on mobile screens, social profile icons, embroidery, and small print."),
        ("Following short-lived visual trends", "Trendy effects date quickly. Build around a distinctive idea instead of the current design fashion."),
    ])
    mistakes_page(c, 4, 5, [
        ("Judging only by personal taste", "The logo must connect with the intended customer and business position, not only the founder's preferences."),
        ("Approving only one layout or colour", "A usable identity needs alternate formats, monochrome versions, and a recognisable icon."),
        ("Accepting incomplete final files", "Without vector masters and organised exports, every future sign, package, or campaign becomes harder."),
    ])
    pricing(c)
    hiring(c)
    worksheet(c)
    files_and_cta(c)
    c.save()
    PUBLIC_OUTPUT.write_bytes(OUTPUT.read_bytes())
    print(OUTPUT)
    print(PUBLIC_OUTPUT)


if __name__ == "__main__":
    build()
