<?php

namespace PloverCRM\Core\CRM\Schemas;

/**
 * Note Schema
 * 
 * Defines the standard structure for note data across all CRM providers
 */
class NoteSchema
{
    /**
     * Required fields
     */
    const REQUIRED_FIELDS = ['id', 'title', 'description', 'created_at'];

    /**
     * All fields with their types
     */
    const FIELDS = [
        'id' => 'integer',
        'subscriber_id' => 'integer',
        'title' => 'string',
        'description' => 'string',
        'type' => 'string',
        'created_by' => 'string',
        'created_at' => 'string',
        'updated_at' => 'string',
    ];

    /**
     * Validate note 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 note data
     */
    public static function normalize(array $data): array
    {
        return [
            'id' => (int) ($data['id'] ?? 0),
            'subscriber_id' => (int) ($data['subscriber_id'] ?? 0),
            'title' => (string) ($data['title'] ?? ''),
            'description' => (string) ($data['description'] ?? ''),
            'type' => (string) ($data['type'] ?? 'note'),
            'created_by' => (string) ($data['created_by'] ?? ''),
            'created_at' => (string) ($data['created_at'] ?? ''),
            'updated_at' => (string) ($data['updated_at'] ?? ''),
        ];
    }
}
