<?php
declare(strict_types=1);

namespace App\Industry;

/**
 * Fixed Phase 2 industry setups. Admins pick one here. Phase 3 Setup
 * lets them rename words, edit stages, and add fields after Publish.
 */
final class Catalog
{
    public const DISTRIBUTION = 'distribution';
    public const SOFTWARE = 'software';
    public const CONTRACT = 'contract';
    public const SERVICE = 'service';

    /** @return list<string> */
    public static function codes(): array
    {
        return [self::DISTRIBUTION, self::SOFTWARE, self::CONTRACT, self::SERVICE];
    }

    public static function isValid(string $code): bool
    {
        return in_array($code, self::codes(), true);
    }

    public static function get(string $code): array
    {
        $all = self::all();
        if (!isset($all[$code])) {
            throw new \InvalidArgumentException('Unknown industry setup.');
        }
        return $all[$code];
    }

    public static function current(): array
    {
        $tenant = current_tenant();
        $code = (string) ($tenant['industry_code'] ?? self::DISTRIBUTION);
        if (!self::isValid($code)) {
            $code = self::DISTRIBUTION;
        }
        return self::get($code);
    }

    public static function currentCode(): string
    {
        return (string) (self::current()['code'] ?? self::DISTRIBUTION);
    }

    public static function isServiceSetup(): bool
    {
        return self::currentCode() === self::SERVICE;
    }

    /** @return array<string, array<string, mixed>> */
    public static function all(): array
    {
        return [
            self::DISTRIBUTION => self::distribution(),
            self::SOFTWARE => self::software(),
            self::CONTRACT => self::contract(),
            self::SERVICE => self::service(),
        ];
    }

    private static function distribution(): array
    {
        return [
            'code' => self::DISTRIBUTION,
            'letter' => 'A',
            'name' => 'Distribution',
            'summary' => 'Office products, wholesale, cleaning supplies',
            'opportunity_label' => 'Opportunities',
            'account_label' => 'Accounts',
            'report_title' => 'Reorders',
            'report_hint' => 'Customers whose last order is older than their reorder cycle.',
            'stages' => [
                ['code' => 'lead', 'name' => 'Lead', 'probability' => 10, 'won' => 0, 'lost' => 0],
                ['code' => 'good_account', 'name' => 'Qualified Account', 'probability' => 25, 'won' => 0, 'lost' => 0],
                ['code' => 'quote', 'name' => 'Quote', 'probability' => 50, 'won' => 0, 'lost' => 0],
                ['code' => 'first_order', 'name' => 'First Order', 'probability' => 75, 'won' => 0, 'lost' => 0],
                ['code' => 'active_customer', 'name' => 'Active Customer', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'reorder', 'name' => 'Reorder / Growth', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'closed_lost', 'name' => 'Closed Lost', 'probability' => 0, 'won' => 0, 'lost' => 1],
            ],
            'account_fields' => [
                ['key' => 'customer_type', 'label' => 'Customer type', 'type' => 'select', 'options' => ['prospect' => 'Prospect', 'active' => 'Active', 'reorder' => 'Reorder']],
                ['key' => 'account_potential', 'label' => 'Account potential', 'type' => 'money'],
                ['key' => 'product_categories', 'label' => 'Product categories', 'type' => 'text'],
                ['key' => 'vendor_lines', 'label' => 'Vendor lines', 'type' => 'text'],
                ['key' => 'buying_frequency', 'label' => 'Buying frequency', 'type' => 'select', 'options' => [
                    'weekly' => 'Weekly',
                    'monthly' => 'Monthly',
                    'quarterly' => 'Quarterly',
                    'yearly' => 'Yearly',
                    'as_needed' => 'As needed',
                ]],
                ['key' => 'last_order_at', 'label' => 'Last order date', 'type' => 'date'],
                ['key' => 'reorder_cycle_days', 'label' => 'Reorder cycle (days)', 'type' => 'number'],
                ['key' => 'margin', 'label' => 'Margin (%)', 'type' => 'number'],
                ['key' => 'lost_sales', 'label' => 'Lost sales', 'type' => 'money'],
                ['key' => 'contract_pricing_status', 'label' => 'Contract / pricing status', 'type' => 'select', 'options' => [
                    'none' => 'None',
                    'contract' => 'Contract',
                    'bid' => 'Bid',
                    'special' => 'Special pricing',
                    'expired' => 'Expired',
                ]],
            ],
            'opportunity_fields' => [
                ['key' => 'order_type', 'label' => 'Order type', 'type' => 'select', 'options' => ['first' => 'First order', 'reorder' => 'Reorder']],
            ],
        ];
    }

    private static function software(): array
    {
        return [
            'code' => self::SOFTWARE,
            'letter' => 'B',
            'name' => 'Software',
            'summary' => 'Software and subscriptions',
            'opportunity_label' => 'Opportunities',
            'account_label' => 'Accounts',
            'report_title' => 'Renewals',
            'report_hint' => 'Customers with a renewal date in the next 60 days.',
            'stages' => [
                ['code' => 'lead', 'name' => 'Lead', 'probability' => 10, 'won' => 0, 'lost' => 0],
                ['code' => 'discovery', 'name' => 'Discovery', 'probability' => 20, 'won' => 0, 'lost' => 0],
                ['code' => 'demo', 'name' => 'Demo', 'probability' => 35, 'won' => 0, 'lost' => 0],
                ['code' => 'trial', 'name' => 'Evaluation', 'probability' => 50, 'won' => 0, 'lost' => 0],
                ['code' => 'proposal', 'name' => 'Proposal', 'probability' => 65, 'won' => 0, 'lost' => 0],
                ['code' => 'verbal_yes', 'name' => 'Verbal Commit', 'probability' => 80, 'won' => 0, 'lost' => 0],
                ['code' => 'won', 'name' => 'Closed Won', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'start_renew', 'name' => 'Onboarding / Renewal', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'closed_lost', 'name' => 'Closed Lost', 'probability' => 0, 'won' => 0, 'lost' => 1],
            ],
            'account_fields' => [
                ['key' => 'arr', 'label' => 'ARR', 'type' => 'money'],
                ['key' => 'seats', 'label' => 'Seats / users', 'type' => 'number'],
                ['key' => 'plan', 'label' => 'Plan', 'type' => 'text'],
                ['key' => 'product_interest', 'label' => 'Product / module interest', 'type' => 'text'],
                ['key' => 'current_system', 'label' => 'Current system', 'type' => 'text'],
                ['key' => 'contract_term', 'label' => 'Contract term', 'type' => 'select', 'options' => [
                    'monthly' => 'Monthly',
                    'annual' => 'Annual',
                    'two_year' => '2 year',
                    'three_year' => '3 year',
                ]],
                ['key' => 'renewal_date', 'label' => 'Renewal date', 'type' => 'date'],
            ],
            'opportunity_fields' => [
                ['key' => 'mrr', 'label' => 'MRR', 'type' => 'money'],
                ['key' => 'trial_end_date', 'label' => 'Trial end', 'type' => 'date'],
                ['key' => 'implementation_target', 'label' => 'Implementation target', 'type' => 'date'],
            ],
        ];
    }

    private static function contract(): array
    {
        return [
            'code' => self::CONTRACT,
            'letter' => 'C',
            'name' => 'Contract',
            'summary' => 'Bids, projects, school/government',
            'opportunity_label' => 'Bids',
            'account_label' => 'Accounts',
            'report_title' => 'Bids',
            'report_hint' => 'Open bids by stage and bids due soon.',
            'stages' => [
                ['code' => 'lead', 'name' => 'Lead', 'probability' => 10, 'won' => 0, 'lost' => 0],
                ['code' => 'scope', 'name' => 'Scope', 'probability' => 25, 'won' => 0, 'lost' => 0],
                ['code' => 'estimate', 'name' => 'Estimate / Bid', 'probability' => 40, 'won' => 0, 'lost' => 0],
                ['code' => 'submitted', 'name' => 'Submitted', 'probability' => 55, 'won' => 0, 'lost' => 0],
                ['code' => 'talk_price', 'name' => 'Negotiation', 'probability' => 75, 'won' => 0, 'lost' => 0],
                ['code' => 'won', 'name' => 'Awarded', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'do_the_work', 'name' => 'Fulfillment / Project', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'closed_lost', 'name' => 'Closed Lost', 'probability' => 0, 'won' => 0, 'lost' => 1],
            ],
            'account_fields' => [
                ['key' => 'bid_type', 'label' => 'Buyer type', 'type' => 'select', 'options' => ['school' => 'School', 'government' => 'Government', 'commercial' => 'Commercial']],
                ['key' => 'purchasing_authority', 'label' => 'Purchasing authority', 'type' => 'text'],
                ['key' => 'contract_start_date', 'label' => 'Contract start', 'type' => 'date'],
                ['key' => 'contract_end_date', 'label' => 'Contract end', 'type' => 'date'],
            ],
            'opportunity_fields' => [
                ['key' => 'estimate_ref', 'label' => 'Bid number', 'type' => 'text'],
                ['key' => 'bid_due_date', 'label' => 'Due date', 'type' => 'date'],
                ['key' => 'project_value', 'label' => 'Project value', 'type' => 'money'],
                ['key' => 'products', 'label' => 'Products', 'type' => 'text'],
                ['key' => 'competitors', 'label' => 'Competitors', 'type' => 'text'],
                ['key' => 'documents', 'label' => 'Documents', 'type' => 'text'],
                ['key' => 'compliance_requirements', 'label' => 'Compliance requirements', 'type' => 'text'],
            ],
        ];
    }

    private static function service(): array
    {
        return [
            'code' => self::SERVICE,
            'letter' => 'D',
            'name' => 'Service',
            'summary' => 'Copier dealers, field service, consultants',
            'opportunity_label' => 'Opportunities',
            'account_label' => 'Accounts',
            'report_title' => 'Service visits',
            'report_hint' => 'Upcoming and late site visits.',
            'stages' => [
                ['code' => 'inquiry', 'name' => 'Inquiry', 'probability' => 10, 'won' => 0, 'lost' => 0],
                ['code' => 'qualify', 'name' => 'Qualification', 'probability' => 25, 'won' => 0, 'lost' => 0],
                ['code' => 'site_visit', 'name' => 'Site Visit / Consultation', 'probability' => 40, 'won' => 0, 'lost' => 0],
                ['code' => 'proposal', 'name' => 'Proposal', 'probability' => 55, 'won' => 0, 'lost' => 0],
                ['code' => 'agreement', 'name' => 'Agreement', 'probability' => 80, 'won' => 0, 'lost' => 0],
                ['code' => 'active_client', 'name' => 'Active Client', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'service_renew', 'name' => 'Service / Renewal', 'probability' => 100, 'won' => 1, 'lost' => 0],
                ['code' => 'closed_lost', 'name' => 'Closed Lost', 'probability' => 0, 'won' => 0, 'lost' => 1],
            ],
            'account_fields' => [
                ['key' => 'service_type', 'label' => 'Service type', 'type' => 'select', 'options' => [
                    'copier' => 'Copier / equipment',
                    'field' => 'Field service',
                    'consulting' => 'Consulting',
                    'agency' => 'Agency',
                    'recurring' => 'Recurring service',
                    'other' => 'Other',
                ]],
                ['key' => 'service_location', 'label' => 'Location', 'type' => 'text'],
                ['key' => 'equipment_assets', 'label' => 'Equipment / assets', 'type' => 'text'],
                ['key' => 'sla', 'label' => 'SLA', 'type' => 'select', 'options' => [
                    '4_hour' => '4 hour',
                    'next_day' => 'Next business day',
                    '48_hour' => '48 hour',
                    'weekly' => 'Weekly',
                    'best_effort' => 'Best effort',
                ]],
                ['key' => 'recurring_value', 'label' => 'Recurring value', 'type' => 'money'],
                ['key' => 'site_notes', 'label' => 'Site notes', 'type' => 'text'],
            ],
            'opportunity_fields' => [
                ['key' => 'site_visit_on', 'label' => 'Appointment', 'type' => 'date'],
                ['key' => 'agreement_months', 'label' => 'Agreement (months)', 'type' => 'number'],
            ],
        ];
    }
}
