package com.nadi.player.nativeplayer.channels;

import org.json.JSONObject;

/**
 * Immutable IPTV channel model.
 *
 * Built from the JSON payload the React/Capacitor layer hands us via
 * {@code NativePlayer.play({ channels: [...] })}. We keep the field set
 * deliberately small — just what the right-side overlay actually renders
 * plus the data needed to swap streams on zap (URL + headers + UA).
 *
 * Renamed from the Step-1 placeholder {@code MockChannel} now that real
 * data flows in from the Xtream / M3U parser.
 */
public final class Channel {

    public final int     number;        // channel number used for UP/DOWN zapping
    public final String  name;          // display name
    public final String  category;      // category key/label (used by category strip)
    public final String  epgNow;        // current programme, nullable
    public final String  epgNext;       // next programme, nullable
    public final boolean favorite;      // → gold star marker in the row
    public final String  streamUrl;     // stream URL to play on selection
    public final String  userAgent;     // optional override, nullable
    /** JSON of {key:value} extra HTTP headers (e.g. Referer), nullable. */
    public final String  headersJson;
    /** Optional logo URL (wired in a future iteration; ignored for now). */
    public final String  logoUrl;

    public Channel(int number, String name, String category,
                   String epgNow, String epgNext, boolean favorite,
                   String streamUrl, String userAgent,
                   String headersJson, String logoUrl) {
        this.number = number;
        this.name = name == null ? "" : name;
        this.category = category == null || category.isEmpty() ? "Other" : category;
        this.epgNow = epgNow;
        this.epgNext = epgNext;
        this.favorite = favorite;
        this.streamUrl = streamUrl;
        this.userAgent = userAgent;
        this.headersJson = headersJson;
        this.logoUrl = logoUrl;
    }

    /** First character of the name, uppercased — used by the logo placeholder
     *  while real logo loading isn't wired in yet. */
    public String logoLetter() {
        if (name.isEmpty()) return "?";
        return name.substring(0, 1).toUpperCase();
    }

    /** Tolerant parser — every field is optional except {@code streamUrl}.
     *  Returns {@code null} if the entry lacks a playable URL. */
    public static Channel fromJson(JSONObject o, int fallbackNumber) {
        if (o == null) return null;
        String url = o.optString("streamUrl", o.optString("url", ""));
        if (url == null || url.isEmpty()) return null;
        return new Channel(
                o.optInt("number", fallbackNumber),
                o.optString("name", ""),
                o.optString("category", "Other"),
                emptyToNull(o.optString("epgNow", "")),
                emptyToNull(o.optString("epgNext", "")),
                o.optBoolean("favorite", false),
                url,
                emptyToNull(o.optString("userAgent", "")),
                o.has("headers") ? o.optJSONObject("headers") != null
                        ? o.optJSONObject("headers").toString() : null : null,
                emptyToNull(o.optString("logoUrl", ""))
        );
    }

    private static String emptyToNull(String s) {
        return s == null || s.isEmpty() ? null : s;
    }
}
