<?php

namespace PloverCRM\Core\CRM\Schemas;

/**
 * Activity Type Schema
 * 
 * Defines the standard structure for activity/note types across all CRM providers
 */
class ActivityTypeSchema
{
    /**
     * Required fields
     */
    const REQUIRED_FIELDS = ['id', 'label', 'icon'];

    /**
     * All fields with their types
     */
    const FIELDS = [
        'id' => 'string',
        'label' => 'string',
        'icon' => 'string',
        'library' => 'string',
    ];

    /**
     * Default icon library
     */
    const DEFAULT_LIBRARY = 'mci';

    /**
     * Extra types that are always included (not CRM-specific)
     */
    const EXTRA_TYPES = [
        ['id' => 'sms', 'label' => 'SMS'],
        ['id' => 'whatsapp', 'label' => 'WhatsApp'],
        ['id' => 'telegram', 'label' => 'Telegram'],
        ['id' => 'template', 'label' => 'Template'],
    ];

    /**
     * Default icon mappings for common activity types
     */
   const DEFAULT_ICONS = [
    'note' => ['icon' => 'note-text'],
    'call' => ['icon' => 'phone'],
    'email' => ['icon' => 'email'],
    'meeting' => ['icon' => 'calendar'],
    'task' => ['icon' => 'checkbox-marked-outline'],
    'sms' => ['icon' => 'message-text'],
    'chat' => ['icon' => 'chat'],

    // Messaging
    'whatsapp' => ['icon' => 'whatsapp'],
    'telegram' => ['icon' => 'telegram-plane', 'library' => 'fa5'],

    // Templates
    'template' => ['icon' => 'content-duplicate'],

    // Quotes
    'quote_sent' => ['icon' => 'file-send'],
    'quote_accepted' => ['icon' => 'check-circle'],
    'quote_refused' => ['icon' => 'close-circle'],

    // Invoices
    'invoice_sent' => ['icon' => 'file-send-outline'],
    'invoice_part_paid' => ['icon' => 'currency-usd'],
    'invoice_paid' => ['icon' => 'check-decagram'],
    'invoice_refunded' => ['icon' => 'cash-refund'],

    // Finance & feedback
    'transaction' => ['icon' => 'bank-transfer'],
    'feedback' => ['icon' => 'message-text'],

    // Social
    'tweet' => ['icon' => 'twitter'],
    'facebook_post' => ['icon' => 'facebook'],
];


    /**
     * Validate activity type data
     */
    public static function validate(array $data): array
    {
        $errors = [];

        foreach (self::REQUIRED_FIELDS as $field) {
            if (!isset($data[$field]) || $data[$field] === null || $data[$field] === '') {
                $errors[] = "Required field '{$field}' is missing or empty";
            }
        }

        return [
            'valid' => empty($errors),
            'errors' => $errors,
        ];
    }

    /**
     * Normalize activity type data
     */
    public static function normalize(array $data): array
    {
        $id = (string) ($data['id'] ?? '');
        
        // Get default icon if not provided
        $defaultIcon = self::DEFAULT_ICONS[$id] ?? ['icon' => 'note-text'];
        
        $icon = (string) ($data['icon'] ?? $defaultIcon['icon']);
        $library = (string) ($data['library'] ?? $defaultIcon['library'] ?? self::DEFAULT_LIBRARY);
        
        $result = [
            'id' => $id,
            'label' => (string) ($data['label'] ?? $id),
            'icon' => $icon,
        ];
        
        // Only include library if it's not the default (mci)
        if ($library !== self::DEFAULT_LIBRARY) {
            $result['library'] = $library;
        }
        
        return $result;
    }

    /**
     * Get default icon for an activity type
     */
    public static function getDefaultIcon(string $typeId): array
    {
        return self::DEFAULT_ICONS[$typeId] ?? ['icon' => 'note-text', 'library' => 'mci'];
    }

    /**
     * Get normalized extra types
     */
    public static function getExtraTypes(): array
    {
        $types = [];
        
        foreach (self::EXTRA_TYPES as $type) {
            $types[] = self::normalize($type);
        }
        
        return $types;
    }
}
