import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import type { SubscriptionState } from '@/types';
import { subscriptionService } from '@/services/subscription.service';

const initialState: SubscriptionState = {
  subscription: null,
  isLoading: false,
  error: null,
};

export const fetchSubscription = createAsyncThunk(
  'subscription/fetch',
  async (_, { rejectWithValue }) => {
    try {
      const response = await subscriptionService.getSubscription();
      return response.data;
    } catch (error: unknown) {
      const err = error as { response?: { data?: { message?: string } } };
      return rejectWithValue(err?.response?.data?.message || 'Failed to fetch subscription');
    }
  },
);

export const startFreeTrial = createAsyncThunk(
  'subscription/startTrial',
  async (_, { rejectWithValue }) => {
    try {
      const response = await subscriptionService.startFreeTrial();
      return response.data;
    } catch (error: unknown) {
      const err = error as { response?: { data?: { message?: string } } };
      return rejectWithValue(err?.response?.data?.message || 'Failed to start trial');
    }
  },
);

export const cancelSubscription = createAsyncThunk(
  'subscription/cancel',
  async (_, { rejectWithValue }) => {
    try {
      const response = await subscriptionService.cancelSubscription();
      return response.data;
    } catch (error: unknown) {
      const err = error as { response?: { data?: { message?: string } } };
      return rejectWithValue(err?.response?.data?.message || 'Failed to cancel subscription');
    }
  },
);

const subscriptionSlice = createSlice({
  name: 'subscription',
  initialState,
  reducers: {
    clearSubscription(state) {
      state.subscription = null;
      state.error = null;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchSubscription.pending, (state) => { state.isLoading = true; state.error = null; })
      .addCase(fetchSubscription.fulfilled, (state, action) => { state.isLoading = false; state.subscription = action.payload; })
      .addCase(fetchSubscription.rejected, (state, action) => { state.isLoading = false; state.error = action.payload as string; });

    builder
      .addCase(startFreeTrial.pending, (state) => { state.isLoading = true; state.error = null; })
      .addCase(startFreeTrial.fulfilled, (state, action) => { state.isLoading = false; state.subscription = action.payload; })
      .addCase(startFreeTrial.rejected, (state, action) => { state.isLoading = false; state.error = action.payload as string; });

    builder
      .addCase(cancelSubscription.pending, (state) => { state.isLoading = true; state.error = null; })
      .addCase(cancelSubscription.fulfilled, (state, action) => { state.isLoading = false; state.subscription = action.payload; })
      .addCase(cancelSubscription.rejected, (state, action) => { state.isLoading = false; state.error = action.payload as string; });
  },
});

export const { clearSubscription } = subscriptionSlice.actions;
export default subscriptionSlice.reducer;
