<?php

declare(strict_types=1);

namespace App\Enum;

enum DeviceModel: string
{
    case V1 = 'v1'; // Waveshare 7.3"  Spectra-6, 800 × 480  native (wide → landscape natural)
    case V2 = 'v2'; // Waveshare 13.3" Spectra-6, 1200 × 1600 native (tall → portrait natural)

    public function nativeWidth(): int
    {
        return match ($this) {
            self::V1 => 800,
            self::V2 => 1200,
        };
    }

    public function nativeHeight(): int
    {
        return match ($this) {
            self::V1 => 480,
            self::V2 => 1600,
        };
    }

    /**
     * The orientation the panel scans natively. V1 is wide-native so its
     * natural mounting is landscape; V2 is tall-native so its natural
     * mounting is portrait. Rendering only rotates the source image when
     * the user's chosen orientation differs from this.
     */
    public function nativeOrientation(): Orientation
    {
        return match ($this) {
            self::V1 => Orientation::Landscape,
            self::V2 => Orientation::Portrait,
        };
    }

    /** User-facing width (pre-rotation) for the given orientation. */
    public function width(Orientation $orientation): int
    {
        return $orientation === $this->nativeOrientation()
            ? $this->nativeWidth()
            : $this->nativeHeight();
    }

    /** User-facing height (pre-rotation) for the given orientation. */
    public function height(Orientation $orientation): int
    {
        return $orientation === $this->nativeOrientation()
            ? $this->nativeHeight()
            : $this->nativeWidth();
    }

    /**
     * Panel-id string the firmware reports via X-Panel-Id on /image polls.
     * Must match the -DPANEL_ID build flag in firmware/platformio.ini.
     */
    public function panelId(): string
    {
        return match ($this) {
            self::V1 => 'waveshare-7.3-spectra6',
            self::V2 => 'waveshare-13.3-spectra6',
        };
    }

    /**
     * Reverse lookup for the X-Panel-Id header. Returns null if the
     * firmware reports a panel-id the server doesn't recognise — the
     * controller treats that as "no panel-id reported" and leaves the
     * device's existing model alone.
     */
    public static function fromPanelId(string $panelId): ?self
    {
        return match ($panelId) {
            'waveshare-7.3-spectra6'  => self::V1,
            'waveshare-13.3-spectra6' => self::V2,
            default                   => null,
        };
    }
}
