// src/client/entities/room.ts
// Room entity - types and store for room management

import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import {
  roomsRepository,
  type RoomResponse,
  type CreateRoomRequest,
  type UpdateRoomRequest,
} from '../shared/api/index';

// Re-export types for backward compatibility
export type Room = RoomResponse;
export type CreateRoomInput = CreateRoomRequest;
export type UpdateRoomInput = UpdateRoomRequest;

export const useRoomStore = defineStore('room', () => {
  // State
  const rooms = ref<Room[]>([]);
  const loading = ref(false);
  const error = ref<string | null>(null);

  // Computed
  const activeRooms = computed(() => rooms.value);

  // Actions
  async function fetchRooms() {
    loading.value = true;
    error.value = null;
    try {
      const data = await roomsRepository.getAll();
      rooms.value = data;
    } catch (err: any) {
      error.value = err.message || 'Failed to fetch rooms';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function fetchRoomById(id: string) {
    loading.value = true;
    error.value = null;
    try {
      const data = await roomsRepository.getById(id);
      // Update or add room to list
      const index = rooms.value.findIndex((r: Room) => r.id === id);
      if (index >= 0) {
        rooms.value[index] = data;
      } else {
        rooms.value.push(data);
      }
      return data;
    } catch (err: any) {
      error.value = err.message || 'Failed to fetch room';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function createRoom(input: CreateRoomInput) {
    loading.value = true;
    error.value = null;
    try {
      const data = await roomsRepository.create(input);
      rooms.value.push(data);
      return data;
    } catch (err: any) {
      error.value = err.message || 'Failed to create room';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function updateRoom(id: string, input: UpdateRoomInput) {
    loading.value = true;
    error.value = null;
    try {
      const data = await roomsRepository.update(id, input);
      const index = rooms.value.findIndex((r: Room) => r.id === id);
      if (index >= 0) {
        rooms.value[index] = data;
      }
      return data;
    } catch (err: any) {
      // Don't set error.value to allow local error handling in pages
      throw err;
    } finally {
      loading.value = false;
    }
  }

  async function deleteRoom(id: string) {
    loading.value = true;
    error.value = null;
    try {
      await roomsRepository.deleteRoom(id);
      const index = rooms.value.findIndex((r: Room) => r.id === id);
      if (index >= 0) {
        rooms.value.splice(index, 1);
      }
    } catch (err: any) {
      error.value = err.message || 'Failed to delete room';
      throw err;
    } finally {
      loading.value = false;
    }
  }

  function clearError() {
    error.value = null;
  }

  return {
    // State
    rooms,
    loading,
    error,
    // Computed
    activeRooms,
    // Actions
    fetchRooms,
    fetchRoomById,
    createRoom,
    updateRoom,
    deleteRoom,
    clearError,
  };
});
