<?php

namespace App\Services\Policy;

use Illuminate\Support\Facades\Log;

class PolicyContentService
{
    private const TYPES = [
        'school' => 'school_policy.html',
        'picture' => 'picture_policy.html',
    ];

    public function getPolicy(string $type): array
    {
        $type = strtolower(trim($type));
        if (!isset(self::TYPES[$type])) {
            throw new \InvalidArgumentException('Unsupported policy type.');
        }

        $filename = self::TYPES[$type];
        $path = resource_path('policies/' . $filename);

        if (!is_file($path)) {
            Log::warning('Policy file missing.', ['type' => $type, 'path' => $path]);
            return [
                'type' => $type,
                'title' => $this->titleFor($type),
                'content' => '',
                'format' => 'html',
                'source' => $path,
                'updated_at' => null,
            ];
        }

        return [
            'type' => $type,
            'title' => $this->titleFor($type),
            'content' => (string) file_get_contents($path),
            'format' => 'html',
            'source' => $path,
            'updated_at' => date('Y-m-d H:i:s', filemtime($path)),
        ];
    }

    private function titleFor(string $type): string
    {
        return $type === 'picture' ? 'Picture Policy' : 'School Policy';
    }
}
