'use client';

import { create } from 'zustand';

export interface ActiveVideo {
  streamUrl: string;
  title: string;
  poster?: string;
  contentId: string;
  contentType: 'movie' | 'series' | 'live';
  categoryId?: string;
  seriesId?: string;
  currentTime?: number;
  duration?: number;
}

interface PlayerState {
  activeVideo: ActiveVideo | null;
  isMinimized: boolean;
  isPlaying: boolean;
  isPipActive: boolean;
  
  // Actions
  playVideo: (video: ActiveVideo) => void;
  minimizeVideo: () => void;
  maximizeVideo: () => void;
  closeVideo: () => void;
  updateTime: (currentTime: number, duration: number) => void;
  setIsPlaying: (isPlaying: boolean) => void;
  setPipActive: (isActive: boolean) => void;
}

export const usePlayerStore = create<PlayerState>((set) => ({
  activeVideo: null,
  isMinimized: false,
  isPlaying: false,
  isPipActive: false,

  playVideo: (video) => set({
    activeVideo: video,
    isMinimized: false,
    isPlaying: true,
    isPipActive: false
  }),

  minimizeVideo: () => set({ isMinimized: true }),

  maximizeVideo: () => set({ 
    isMinimized: false,
    isPipActive: false
  }),

  closeVideo: () => set({
    activeVideo: null,
    isMinimized: false,
    isPlaying: false,
    isPipActive: false
  }),

  updateTime: (currentTime, duration) => set((state) => {
    if (!state.activeVideo) return {};
    return {
      activeVideo: {
        ...state.activeVideo,
        currentTime,
        duration
      }
    };
  }),

  setIsPlaying: (isPlaying) => set({ isPlaying }),

  setPipActive: (isActive) => set({ isPipActive: isActive })
}));
