<?php

function mdw_document_templates(): array {
    return [
        'service-agreement' => [
            'name' => 'Service Agreement',
            'category' => 'Contracts',
            'quick' => true,
            'description' => 'General web/software development agreement with scope, payment, ownership, revisions, and third-party responsibility wording.',
            'fields' => [
                'client_name' => ['label' => 'Client Name', 'type' => 'text', 'default' => ''],
                'business_name' => ['label' => 'Business Name', 'type' => 'text', 'default' => ''],
                'client_address' => ['label' => 'Client Address', 'type' => 'textarea', 'default' => ''],
                'client_email' => ['label' => 'Client Email', 'type' => 'text', 'default' => ''],
                'project_name' => ['label' => 'Project Name', 'type' => 'text', 'default' => ''],
                'agreement_date' => ['label' => 'Agreement Date', 'type' => 'text', 'default' => date('F j, Y')],
                'scope_of_work' => ['label' => 'Scope of Work', 'type' => 'textarea', 'default' => ''],
                'excluded_items' => ['label' => 'Excluded / Out-of-Scope Items', 'type' => 'textarea', 'default' => 'Items not specifically listed in the scope are not included and may require a written change request.'],
                'project_cost' => ['label' => 'Total Project Cost', 'type' => 'text', 'default' => '$'],
                'deposit' => ['label' => 'Deposit Required', 'type' => 'text', 'default' => '$'],
                'balance_terms' => ['label' => 'Balance Due / Payment Terms', 'type' => 'textarea', 'default' => 'Remaining balance is due before final delivery, launch, or transfer of completed files unless otherwise agreed in writing.'],
                'revision_terms' => ['label' => 'Revision Terms', 'type' => 'textarea', 'default' => 'Reasonable revisions related to the agreed project scope are included. Major redesigns, new features, or new requirements may require additional charges.'],
                'timeline' => ['label' => 'Estimated Timeline', 'type' => 'text', 'default' => ''],
                'support_terms' => ['label' => 'Support / Maintenance Terms', 'type' => 'textarea', 'default' => 'Ongoing support, maintenance, monitoring, backups, future modifications, and hosting support are not included unless covered by a separate written agreement.'],
            ],
            'sections' => [
                'Client Information' => ['client_name','business_name','client_address','client_email','project_name','agreement_date'],
                'Scope of Work' => ['scope_of_work','excluded_items'],
                'Payment Terms' => ['project_cost','deposit','balance_terms'],
                'Timeline and Revisions' => ['timeline','revision_terms'],
                'Support and Maintenance' => ['support_terms'],
            ],
            'legal_note' => 'Client is responsible for hosting, domain registration, email service, renewals, and third-party provider accounts. Murdock DevWorks may assist with setup and deployment but is not responsible for third-party outages, billing issues, account suspensions, provider limitations, or hosting failures.'
        ],
        'hosting-responsibility' => [
            'name' => 'Hosting Setup & Responsibility Agreement',
            'category' => 'Setup & Access',
            'description' => 'Clarifies that the client owns and pays for hosting/domain/email while MDW may assist with setup and deployment.',
            'fields' => [
                'client_name' => ['label' => 'Client Name', 'type' => 'text', 'default' => ''],
                'business_name' => ['label' => 'Business Name', 'type' => 'text', 'default' => ''],
                'domain_name' => ['label' => 'Domain Name', 'type' => 'text', 'default' => ''],
                'hosting_provider' => ['label' => 'Hosting Provider', 'type' => 'text', 'default' => ''],
                'email_provider' => ['label' => 'Email Provider', 'type' => 'text', 'default' => ''],
                'setup_services' => ['label' => 'Setup Services MDW May Assist With', 'type' => 'textarea', 'default' => 'Website upload/deployment\nDatabase setup/import\nDNS guidance\nSSL setup guidance\nEmail configuration assistance\nBasic launch testing'],
                'client_responsibility' => ['label' => 'Client Responsibilities', 'type' => 'textarea', 'default' => 'Client owns and pays for hosting, domain registration, email service, renewals, third-party subscriptions, and provider accounts. Client is responsible for keeping accounts active and providing needed access.'],
                'provider_disclaimer' => ['label' => 'Third-Party Provider Disclaimer', 'type' => 'textarea', 'default' => 'Murdock DevWorks is not responsible for hosting outages, provider downtime, email delivery failures, account suspensions, billing disputes, domain renewal failures, or hosting company limitations.'],
            ],
            'sections' => [
                'Client / Account Information' => ['client_name','business_name','domain_name','hosting_provider','email_provider'],
                'Setup Assistance' => ['setup_services'],
                'Client Responsibility' => ['client_responsibility'],
                'Third-Party Provider Disclaimer' => ['provider_disclaimer'],
            ],
        ],
        'project-proposal' => [
            'name' => 'Project Proposal',
            'category' => 'Proposals',
            'quick' => true,
            'description' => 'A branded proposal for quoting website, web app, or software work.',
            'fields' => [
                'prepared_for' => ['label' => 'Prepared For', 'type' => 'text', 'default' => ''],
                'project_title' => ['label' => 'Project Title', 'type' => 'text', 'default' => ''],
                'proposal_date' => ['label' => 'Proposal Date', 'type' => 'text', 'default' => date('F j, Y')],
                'overview' => ['label' => 'Project Overview', 'type' => 'textarea', 'default' => ''],
                'deliverables' => ['label' => 'Deliverables', 'type' => 'textarea', 'default' => ''],
                'phases' => ['label' => 'Phases / Milestones', 'type' => 'textarea', 'default' => 'Planning and content review\nDesign / layout\nDevelopment\nTesting\nLaunch assistance'],
                'timeline' => ['label' => 'Estimated Timeline', 'type' => 'text', 'default' => ''],
                'price' => ['label' => 'Estimated Price', 'type' => 'text', 'default' => '$'],
                'notes' => ['label' => 'Notes', 'type' => 'textarea', 'default' => 'Final scope and payment terms should be confirmed in a service agreement before work begins.'],
            ],
            'sections' => [
                'Proposal Information' => ['prepared_for','project_title','proposal_date'],
                'Project Overview' => ['overview'],
                'Deliverables' => ['deliverables'],
                'Phases / Milestones' => ['phases'],
                'Timeline and Pricing' => ['timeline','price','notes'],
            ],
        ],
        'project-intake' => [
            'name' => 'Client Intake Form',
            'category' => 'Client Forms',
            'quick' => true,
            'description' => 'Collect project goals, hosting/domain information, branding preferences, features, and budget.',
            'fields' => [
                'client_name' => ['label' => 'Client / Business Name', 'type' => 'text', 'default' => ''],
                'contact' => ['label' => 'Contact Information', 'type' => 'textarea', 'default' => 'Name:\nEmail:\nPhone:'],
                'project_type' => ['label' => 'Project Type', 'type' => 'text', 'default' => 'Website / Web App / Support'],
                'goals' => ['label' => 'Project Goals', 'type' => 'textarea', 'default' => ''],
                'features' => ['label' => 'Requested Features', 'type' => 'textarea', 'default' => ''],
                'hosting' => ['label' => 'Hosting / Domain Notes', 'type' => 'textarea', 'default' => ''],
                'branding' => ['label' => 'Branding Preferences / Example Sites', 'type' => 'textarea', 'default' => ''],
                'timeline' => ['label' => 'Timeline / Deadline', 'type' => 'text', 'default' => ''],
                'budget' => ['label' => 'Budget Range', 'type' => 'text', 'default' => ''],
            ],
            'sections' => [
                'Client Information' => ['client_name','contact'],
                'Project Type and Goals' => ['project_type','goals'],
                'Requested Features' => ['features'],
                'Hosting / Domain / Branding' => ['hosting','branding'],
                'Timeline and Budget' => ['timeline','budget'],
            ],
        ],
        'change-request' => [
            'name' => 'Change Request Form',
            'category' => 'Client Forms',
            'description' => 'Document scope changes, added cost, added time, and client approval.',
            'fields' => [
                'client_name' => ['label' => 'Client Name', 'type' => 'text', 'default' => ''],
                'project_name' => ['label' => 'Project Name', 'type' => 'text', 'default' => ''],
                'request_date' => ['label' => 'Request Date', 'type' => 'text', 'default' => date('F j, Y')],
                'requested_change' => ['label' => 'Requested Change', 'type' => 'textarea', 'default' => ''],
                'reason' => ['label' => 'Reason / Notes', 'type' => 'textarea', 'default' => ''],
                'cost_impact' => ['label' => 'Additional Cost', 'type' => 'text', 'default' => '$'],
                'time_impact' => ['label' => 'Additional Time Required', 'type' => 'text', 'default' => ''],
                'approval' => ['label' => 'Approval Notes', 'type' => 'textarea', 'default' => 'Client approval is required before this change is added to the project scope.'],
            ],
            'sections' => [
                'Change Request Information' => ['client_name','project_name','request_date'],
                'Requested Change' => ['requested_change','reason'],
                'Project Impact' => ['cost_impact','time_impact','approval'],
            ],
        ],
        'maintenance-agreement' => [
            'name' => 'Website Maintenance Agreement',
            'category' => 'Contracts',
            'description' => 'Recurring or limited maintenance agreement for updates, support, and minor changes.',
            'fields' => [
                'client_name' => ['label' => 'Client Name', 'type' => 'text', 'default' => ''],
                'website' => ['label' => 'Website / Project', 'type' => 'text', 'default' => ''],
                'monthly_fee' => ['label' => 'Monthly Fee', 'type' => 'text', 'default' => '$'],
                'included_services' => ['label' => 'Included Services', 'type' => 'textarea', 'default' => 'Minor content updates\nSoftware update assistance\nBasic troubleshooting\nBackup assistance if available'],
                'excluded_services' => ['label' => 'Excluded Services', 'type' => 'textarea', 'default' => 'Major redesigns, new feature development, custom integrations, third-party provider outages, emergency recovery, and hosting company issues are not included unless separately quoted.'],
                'cancellation' => ['label' => 'Cancellation Terms', 'type' => 'textarea', 'default' => 'Either party may cancel maintenance services with written notice.'],
            ],
            'sections' => [
                'Maintenance Agreement' => ['client_name','website','monthly_fee'],
                'Included Services' => ['included_services'],
                'Excluded Services' => ['excluded_services'],
                'Cancellation' => ['cancellation'],
            ],
        ],
        'software-license' => [
            'name' => 'Software License Agreement',
            'category' => 'Software Licenses',
            'description' => 'Basic license terms for MDW software products such as ClearChat or PremiumDB.',
            'fields' => [
                'product_name' => ['label' => 'Product Name', 'type' => 'text', 'default' => ''],
                'licensee' => ['label' => 'Licensee / Business', 'type' => 'text', 'default' => ''],
                'license_type' => ['label' => 'License Type', 'type' => 'text', 'default' => 'Single-site license'],
                'permitted_use' => ['label' => 'Permitted Use', 'type' => 'textarea', 'default' => 'Purchaser may install and use the software according to the purchased licensing terms.'],
                'restrictions' => ['label' => 'Restrictions', 'type' => 'textarea', 'default' => 'Purchaser may not redistribute, resell, claim ownership of original source code, or remove copyright notices unless specifically permitted in writing.'],
                'warranty' => ['label' => 'Warranty Disclaimer', 'type' => 'textarea', 'default' => 'Software is provided as-is without warranties of any kind. Murdock DevWorks is not responsible for data loss, downtime, third-party incompatibility, hosting/server issues, or misuse of the software.'],
            ],
            'sections' => [
                'License Information' => ['product_name','licensee','license_type'],
                'Permitted Use' => ['permitted_use'],
                'Restrictions' => ['restrictions'],
                'Warranty Disclaimer' => ['warranty'],
            ],
        ],
        'invoice' => [
            'name' => 'Invoice Template',
            'category' => 'Billing',
            'quick' => true,
            'description' => 'Simple branded invoice with CSV export support for the accounting program later.',
            'fields' => [
                'invoice_number' => ['label' => 'Invoice Number', 'type' => 'text', 'default' => function_exists('mdw_peek_next_invoice_number') ? mdw_peek_next_invoice_number() : 'MDW-INV-' . date('Y') . '-0001'],
                'invoice_date' => ['label' => 'Invoice Date', 'type' => 'text', 'default' => date('F j, Y')],
                'client_name' => ['label' => 'Client Name', 'type' => 'text', 'default' => ''],
                'project_name' => ['label' => 'Project / Description', 'type' => 'text', 'default' => ''],
                'line_items' => ['label' => 'Line Items', 'type' => 'textarea', 'default' => "Website / software services | 1 | 0.00"],
                'subtotal' => ['label' => 'Subtotal', 'type' => 'text', 'default' => '0.00'],
                'tax' => ['label' => 'Tax', 'type' => 'text', 'default' => '0.00'],
                'total_due' => ['label' => 'Total Due', 'type' => 'text', 'default' => '0.00'],
                'due_date' => ['label' => 'Payment Due By', 'type' => 'text', 'default' => ''],
                'payment_notes' => ['label' => 'Payment Notes', 'type' => 'textarea', 'default' => 'Thank you for your business.'],
                'payment_status' => ['label' => 'Payment Status', 'type' => 'text', 'default' => 'Draft'],
            ],
            'sections' => [
                'Invoice Information' => ['invoice_number','invoice_date','client_name','project_name','due_date','payment_status'],
                'Invoice Amounts' => ['line_items','subtotal','tax','total_due','payment_notes'],
            ],
        ],
        'pricing-guide' => [
            'name' => 'Pricing Guide',
            'category' => 'Billing',
            'quick' => true,
            'description' => 'A branded one-page pricing reference for websites, web apps, setup work, and maintenance.',
            'fields' => [
                'prepared_for' => ['label' => 'Prepared For', 'type' => 'text', 'default' => 'Internal Use'],
                'effective_date' => ['label' => 'Effective Date', 'type' => 'text', 'default' => date('F j, Y')],
                'intro' => ['label' => 'Intro Text', 'type' => 'textarea', 'default' => 'Murdock DevWorks builds practical software, self-hosted tools, and digital products for small businesses and real-world workflows. Pricing below is a starting guide and final quotes depend on project scope, integrations, content, and support needs.'],
                'simple_site' => ['label' => 'Simple Business Website', 'type' => 'text', 'default' => '$500 - $1,500'],
                'custom_site' => ['label' => 'Custom Site With Admin', 'type' => 'text', 'default' => '$1,500 - $4,000'],
                'web_app' => ['label' => 'Custom Web App / Workflow Tool', 'type' => 'text', 'default' => '$3,000 - $15,000+'],
                'maintenance' => ['label' => 'Monthly Maintenance', 'type' => 'text', 'default' => '$50 - $250/month'],
                'setup_migration' => ['label' => 'Setup / Migration Assistance', 'type' => 'text', 'default' => '$150 - $750'],
                'custom_integrations' => ['label' => 'Custom Integrations', 'type' => 'text', 'default' => 'Quoted by scope or hourly'],
                'terms' => ['label' => 'Terms / Notes', 'type' => 'textarea', 'default' => 'Recommended project workflow: written scope, milestone payment, review period, and separate maintenance/support agreement if ongoing service is needed.'],
            ],
            'sections' => [
                'Pricing Guide' => ['prepared_for','effective_date','intro'],
                'Starting Price Guide' => ['simple_site','custom_site','web_app','maintenance','setup_migration','custom_integrations'],
                'Notes' => ['terms'],
            ],
        ],
    ];
}

function mdw_document_categories(): array {
    $cats = [];
    foreach (mdw_document_templates() as $template) $cats[] = $template['category'] ?? 'General';
    $cats = array_values(array_unique($cats));
    sort($cats);
    return $cats;
}

function mdw_template_by_key(string $key): ?array {
    $templates = mdw_document_templates();
    return $templates[$key] ?? null;
}

function mdw_doc_display_value(array $doc, string $key): string {
    return (string)($doc['fields'][$key] ?? '');
}
