#!/bin/bash

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}===========================================${NC}"
echo -e "${BLUE}Phase 1, 2 & 3 Redux Migration Test Runner${NC}"
echo -e "${BLUE}===========================================${NC}\n"

# Check if we're in the right directory
if [ ! -f "package.json" ]; then
    echo -e "${RED}Error: package.json not found. Please run this script from jam-ui directory.${NC}"
    exit 1
fi

echo -e "${YELLOW}Installing dependencies (if needed)...${NC}"
npm install --silent

echo -e "\n${BLUE}Running Unit Tests...${NC}\n"

# Run sessionUISlice tests
echo -e "${YELLOW}Testing sessionUISlice (Phase 1 - Modals)...${NC}"
npm run test:unit -- src/store/features/__tests__/sessionUISlice.test.js --verbose

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ sessionUISlice tests passed!${NC}\n"
else
    echo -e "${RED}✗ sessionUISlice tests failed!${NC}\n"
    exit 1
fi

# Run activeSessionSlice tests
echo -e "${YELLOW}Testing activeSessionSlice (Phase 2 & 3 - Lifecycle & Entities)...${NC}"
npm run test:unit -- src/store/features/__tests__/activeSessionSlice.test.js --verbose

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ activeSessionSlice tests passed!${NC}\n"
else
    echo -e "${RED}✗ activeSessionSlice tests failed!${NC}\n"
    exit 1
fi

echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}All automated tests passed! ✓${NC}"
echo -e "${BLUE}========================================${NC}\n"

echo -e "${YELLOW}Next steps:${NC}"
echo -e "  1. Run manual tests: See ${BLUE}docs/TESTING_GUIDE_PHASE1_AND_2.md${NC}"
echo -e "  2. Review Phase 3 docs: See ${BLUE}docs/PHASE3_MIGRATION_SUMMARY.md${NC}"
echo -e "  3. Start the app: ${BLUE}npm start${NC}"
echo -e "  4. Open Redux DevTools and verify state changes"
echo -e "  5. Test modals, session lifecycle, jam tracks, backing tracks\n"

echo -e "${GREEN}Test coverage report:${NC}"
npm run test:unit -- src/store/features/__tests__/ --coverage --collectCoverageFrom='src/store/features/*.js' 2>/dev/null | grep -A 20 "Coverage summary"

echo -e "\n${YELLOW}View full coverage:${NC} open coverage/lcov-report/index.html\n"
