<?php
/**
 * FAQ Schema Component
 * Generates FAQPage structured data for FAQ sections
 *
 * Usage:
 * $faqs = [
 *     ['question' => 'What is web scraping?', 'answer' => 'Web scraping is...'],
 *     ['question' => 'How much does it cost?', 'answer' => 'Pricing varies...']
 * ];
 * include($_SERVER['DOCUMENT_ROOT'] . '/includes/schema/faq-schema.php');
 */

/**
 * Generate FAQPage Schema JSON-LD
 *
 * @param array $faqs Array of Q&A pairs with 'question' and 'answer' keys
 * @param string|null $pageUrl Optional canonical URL for the FAQ page
 * @param string|null $pageName Optional name for the FAQ page
 * @return string JSON-LD script tag
 */
function generateFAQSchema($faqs, $pageUrl = null, $pageName = null) {
    if (empty($faqs)) {
        return '';
    }

    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'FAQPage'
    ];

    // Add page identifier if URL provided
    if ($pageUrl) {
        $schema['@id'] = $pageUrl . '#faqpage';
        $schema['url'] = $pageUrl;
    }

    // Add page name if provided
    if ($pageName) {
        $schema['name'] = $pageName;
    }

    // Build main entity array
    $schema['mainEntity'] = array_map(function($faq) {
        return [
            '@type' => 'Question',
            'name' => $faq['question'],
            'acceptedAnswer' => [
                '@type' => 'Answer',
                'text' => $faq['answer']
            ]
        ];
    }, $faqs);

    return '<script type="application/ld+json">' . "\n" .
           json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n" .
           '</script>';
}

/**
 * Common FAQs for different page types
 */
$commonFAQs = [
    'general' => [
        [
            'question' => 'What is web scraping and how can it benefit my business?',
            'answer' => 'Web scraping is the automated process of extracting data from websites. It benefits businesses by providing competitive intelligence, automating data collection, enabling real-time price monitoring, and supporting strategic decision-making with accurate market data.'
        ],
        [
            'question' => 'Is web scraping legal in the UK?',
            'answer' => 'Yes, web scraping is legal in the UK when conducted properly. We ensure full compliance with UK Data Protection Act 2018, GDPR, website terms of service, and industry best practices. We only collect publicly available data and respect robots.txt directives.'
        ],
        [
            'question' => 'How do you ensure data accuracy?',
            'answer' => 'We maintain a 99.8% accuracy rate through advanced validation algorithms, multi-layer verification processes, regular monitoring, and comprehensive testing before delivery. Each dataset undergoes rigorous quality assurance checks.'
        ],
        [
            'question' => 'What data formats do you provide?',
            'answer' => 'We deliver data in multiple formats including Excel (XLSX/XLS), CSV, JSON, XML, SQL database dumps, and custom formats. We also offer real-time data feeds and API access for ongoing projects.'
        ],
        [
            'question' => 'How much do your services cost?',
            'answer' => 'Pricing varies based on project complexity: Simple extraction projects start from £500-£2,000, medium complexity projects range from £2,000-£10,000, and enterprise solutions are £10,000+. We provide custom quotes based on your specific requirements.'
        ]
    ],
    'pricing' => [
        [
            'question' => 'What factors affect the cost of web scraping services?',
            'answer' => 'Costs depend on several factors: the number and complexity of target websites, data volume required, frequency of updates, anti-bot measures to navigate, data cleaning requirements, and delivery format preferences.'
        ],
        [
            'question' => 'Do you offer monthly retainer packages?',
            'answer' => 'Yes, we offer flexible monthly retainer packages for ongoing data collection needs. These include regular updates, priority support, and often provide better value than one-off projects for continuous monitoring requirements.'
        ],
        [
            'question' => 'Is there a minimum project value?',
            'answer' => 'Our minimum project value is £500. This ensures we can deliver quality work with proper attention to accuracy, compliance, and client requirements.'
        ]
    ],
    'compliance' => [
        [
            'question' => 'How do you handle GDPR compliance?',
            'answer' => 'GDPR compliance is central to our operations. We only collect personal data when legally justified, follow data minimisation principles, implement robust security measures, maintain clear data retention policies, and respect data subject rights.'
        ],
        [
            'question' => 'Do you scrape personal data?',
            'answer' => 'We only collect personal data when there is a legitimate legal basis, such as legitimate business interests or consent. We follow strict GDPR guidelines and advise clients on compliant data collection strategies.'
        ],
        [
            'question' => 'What security measures do you have in place?',
            'answer' => 'We implement enterprise-grade security including encrypted data transfer (SSL/TLS), secure data storage, access controls, and regular security audits.'
        ]
    ],
    'property-data' => [
        [
            'question' => 'Can you extract data from Rightmove and Zoopla?',
            'answer' => 'Yes, we can extract publicly available property data from UK property portals including Rightmove, Zoopla, OnTheMarket, and others. We ensure compliance with each platform\'s terms of service and UK data protection laws.'
        ],
        [
            'question' => 'What property data can you collect?',
            'answer' => 'We can collect property listings, prices, property features, location data, historical price changes, rental yields, local area information, and market trends. Custom data requirements can be discussed during consultation.'
        ],
        [
            'question' => 'How often can property data be updated?',
            'answer' => 'We offer daily, weekly, or monthly property data updates depending on your needs. Real-time monitoring is also available for time-sensitive market intelligence requirements.'
        ]
    ],
    'financial-data' => [
        [
            'question' => 'Do you provide FCA-compliant financial data services?',
            'answer' => 'Yes, our financial data services are designed with FCA regulations in mind. We help hedge funds, asset managers, and investment firms collect market data while maintaining compliance with applicable financial regulations.'
        ],
        [
            'question' => 'What types of alternative data do you provide?',
            'answer' => 'We provide various alternative data sources including web traffic data, sentiment analysis, pricing data, job posting trends, satellite imagery analysis, and custom data feeds tailored to investment strategies.'
        ],
        [
            'question' => 'Can you provide historical financial data?',
            'answer' => 'Yes, we can extract and compile historical financial data where publicly available. This includes historical pricing, company filings, news archives, and market trend data for backtesting and analysis purposes.'
        ]
    ]
];

// If $faqs is set, output the schema automatically
if (isset($faqs) && is_array($faqs)) {
    echo generateFAQSchema(
        $faqs,
        $faqPageUrl ?? null,
        $faqPageName ?? null
    );
}
