import { createSlice, createSelector } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

interface IPersonalVideo {
    generatingVideo: boolean
    videoId: string
    videoUrl: string
    createdDate: number
    isVideoPlaying: boolean
}

const initialState: IPersonalVideo = {
    generatingVideo: false,
    videoId: "",
    videoUrl: "",
    createdDate: 0,
    isVideoPlaying: false
}

const personalVideoSlice = createSlice({
    name: 'personalVideo',
    initialState,
    reducers: {
        updateStatus(state, action: PayloadAction<Pick<IPersonalVideo, 'generatingVideo' | 'videoId'>>) {
            return { ...state, ...action.payload, createdDate: new Date().getTime() }
        },
        updateUrl(state, action: PayloadAction<string>) {
            return { ...state, videoUrl: action.payload }
        },
        updateIsVideoPlaying(state, action: PayloadAction<boolean>) {
            return { ...state, isVideoPlaying: action.payload }
        }
    },
    extraReducers: (builder) => builder.addCase('reset', () => initialState),
})

export const { actions } = personalVideoSlice
export const selectPersonalVideo = createSelector(
    (state: { personalVideo: IPersonalVideo }) => state.personalVideo,
    (personalVideo) => personalVideo
)
export default personalVideoSlice.reducer
