<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('legal_pages')) {
            return;
        }

        $now = now();
        $company = 'Lakhsman Wellness Private Limited';
        $address = 'Lakhsman Wellness Private Limited, Hutpa, Hazaribag, Jharkhand, INDIA (825313)';
        $phone = '8234673299';
        $email = 'hy@lakhsmanwelleness.in';

        $pages = [
            [
                'slug' => 'terms-and-conditions',
                'title' => 'Terms and Conditions',
                'body' => "<h2>Terms and Conditions</h2><p>By using the website and panel services of {$company}, you agree to these terms.</p><ul><li>All registration details must be correct and verifiable.</li><li>Company may suspend accounts for fraud, abuse, or policy violations.</li><li>Payout, wallet, and commission decisions are governed by approved business rules.</li><li>Disputes are handled under applicable Indian laws and jurisdiction.</li></ul><p>Support: {$phone} | {$email}</p>",
                'meta_title' => 'Terms and Conditions | Lakhsman Wellness',
                'meta_description' => 'Terms and conditions for using Lakhsman Wellness services.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 1,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'privacy-policy',
                'title' => 'Privacy Policy',
                'body' => "<h2>Privacy Policy</h2><p>{$company} collects only required personal and transactional data for secure operations.</p><ul><li>Data is used for KYC, support, payout processing, and compliance.</li><li>Data access is restricted and audited.</li><li>No unauthorized sale of personal data is permitted.</li><li>Lawful requests from regulators or authorities may be honored.</li></ul><p>Registered Office: {$address}</p>",
                'meta_title' => 'Privacy Policy | Lakhsman Wellness',
                'meta_description' => 'How Lakhsman Wellness collects and protects user data.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 2,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'refund-policy',
                'title' => 'Refund Policy',
                'body' => "<h2>Refund Policy</h2><p>Refunds are processed only for eligible orders and verified payment references.</p><ul><li>Refund timelines depend on payment mode and banking network.</li><li>Used or ineligible products may be declined as per policy.</li><li>Income generated from refunded orders may be reversed from wallets.</li><li>Refund claims must include order and payment references.</li></ul><p>Refund desk: {$email}</p>",
                'meta_title' => 'Refund Policy | Lakhsman Wellness',
                'meta_description' => 'Refund terms for products and services at Lakhsman Wellness.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 3,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'cancellation-policy',
                'title' => 'Cancellation Policy',
                'body' => "<h2>Cancellation Policy</h2><p>Orders can be cancelled before dispatch or approval state as per process status.</p><ul><li>After dispatch, cancellation may convert to return workflow.</li><li>Fraud or duplicate transactions can be cancelled by company.</li><li>Associated BV and commissions can be reversed.</li><li>Processing charges may apply where legally permitted.</li></ul><p>Helpdesk: {$phone}</p>",
                'meta_title' => 'Cancellation Policy | Lakhsman Wellness',
                'meta_description' => 'Cancellation terms for Lakhsman Wellness orders.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 4,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'shipping-and-delivery-policy',
                'title' => 'Shipping and Delivery Policy',
                'body' => "<h2>Shipping and Delivery Policy</h2><p>Delivery timelines vary by location, stock availability, and logistics coverage.</p><ul><li>Address and contact details must be accurate.</li><li>Tracking is shared where available.</li><li>Damage or shortage must be reported within defined timeline.</li><li>Force majeure events can impact standard timelines.</li></ul><p>Office: {$address}</p>",
                'meta_title' => 'Shipping and Delivery Policy | Lakhsman Wellness',
                'meta_description' => 'Shipping and delivery policy for Lakhsman Wellness.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 5,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'income-disclaimer',
                'title' => 'Income Disclaimer',
                'body' => "<h2>Income Disclaimer</h2><p>Income in direct selling depends on effort, product movement, compliance, and team quality.</p><ul><li>No guaranteed earning is offered.</li><li>Historical examples are illustrative and non-binding.</li><li>Policy breaches may lead to payout hold or reversal.</li></ul><p>For clarifications write to {$email}.</p>",
                'meta_title' => 'Income Disclaimer | Lakhsman Wellness',
                'meta_description' => 'Income disclaimer for members and franchise partners.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 6,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'grievance-redressal',
                'title' => 'Grievance Redressal',
                'body' => "<h2>Grievance Redressal</h2><p>Submit grievances for product, payment, KYC, withdrawal, or service concerns.</p><ul><li>Email: {$email}</li><li>Phone: {$phone}</li><li>Address: {$address}</li></ul><p>Valid complaints are acknowledged and processed within reasonable timelines.</p>",
                'meta_title' => 'Grievance Redressal | Lakhsman Wellness',
                'meta_description' => 'Official grievance redressal process for Lakhsman Wellness.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 7,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'kyc-policy',
                'title' => 'KYC Policy',
                'body' => "<h2>KYC Policy</h2><p>KYC is mandatory for withdrawals, compliance checks, and account integrity.</p><ul><li>Identity and bank records must match submitted profile details.</li><li>Forged or mismatched documents can trigger blocking and escalation.</li><li>Reverification may be required for suspicious account activity.</li></ul><p>KYC support: {$email}</p>",
                'meta_title' => 'KYC Policy | Lakhsman Wellness',
                'meta_description' => 'KYC rules and verification standards for Lakhsman Wellness accounts.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 8,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'aml-policy',
                'title' => 'AML Policy',
                'body' => "<h2>Anti-Money Laundering (AML) Policy</h2><p>{$company} prohibits money laundering, impersonation, and suspicious payment patterns.</p><ul><li>High-risk transactions are flagged for review.</li><li>Repeated suspicious behavior may result in payout freeze.</li><li>Company cooperates with lawful authority requests.</li></ul><p>Compliance email: {$email}</p>",
                'meta_title' => 'AML Policy | Lakhsman Wellness',
                'meta_description' => 'Anti-money laundering controls followed by Lakhsman Wellness.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 9,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'cookie-policy',
                'title' => 'Cookie Policy',
                'body' => "<h2>Cookie Policy</h2><p>Cookies are used for session security, preferences, and performance analytics.</p><ul><li>Essential cookies are required for login and secure navigation.</li><li>Users may control non-essential cookies in browser settings.</li><li>Disabling cookies may affect dashboard usability.</li></ul><p>For privacy requests contact {$email}.</p>",
                'meta_title' => 'Cookie Policy | Lakhsman Wellness',
                'meta_description' => 'Cookie usage and controls for Lakhsman Wellness website and panels.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 10,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'payment-policy',
                'title' => 'Payment Policy',
                'body' => "<h2>Payment Policy</h2><p>All payments must be made using approved channels and traceable references.</p><ul><li>Redirect URL results are not considered final proof of success.</li><li>Only verified webhook and reconciliation status are treated as final.</li><li>Duplicate payment callback attempts are blocked and logged.</li><li>UPI fraud checks are applied before crediting wallets.</li></ul><p>Payment support: {$phone}</p>",
                'meta_title' => 'Payment Policy | Lakhsman Wellness',
                'meta_description' => 'Payment verification and gateway policy for Lakhsman Wellness.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 11,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
            [
                'slug' => 'direct-seller-code-of-conduct',
                'title' => 'Direct Seller Code of Conduct',
                'body' => "<h2>Direct Seller Code of Conduct</h2><p>All members and franchise partners must maintain ethical business practices.</p><ul><li>No false claims about product or guaranteed income.</li><li>No pressure-selling, harassment, or misrepresentation.</li><li>Respect anti-spam, data privacy, and consumer protection rules.</li><li>Violations can lead to penalties, suspension, or termination.</li></ul><p>Escalation contact: {$email}</p>",
                'meta_title' => 'Code of Conduct | Lakhsman Wellness',
                'meta_description' => 'Code of conduct for all direct sellers in Lakhsman Wellness network.',
                'canonical_url' => null,
                'status' => 'published',
                'is_indexable' => true,
                'sort_order' => 12,
                'is_active' => true,
                'updated_at' => $now,
                'created_at' => $now,
            ],
        ];

        DB::table('legal_pages')->upsert(
            $pages,
            ['slug'],
            [
                'title',
                'body',
                'meta_title',
                'meta_description',
                'canonical_url',
                'status',
                'is_indexable',
                'sort_order',
                'is_active',
                'updated_at',
            ]
        );

        DB::table('legal_pages')
            ->whereIn('slug', ['shipping-policy', 'code-of-conduct'])
            ->update([
                'is_active' => false,
                'updated_at' => $now,
            ]);
    }

    public function down(): void
    {
        // Intentionally left blank to avoid deleting legal content during rollback.
    }
};
