import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AppPalette {
  final String name;
  final Color primary;
  final Color surface;

  const AppPalette({
    required this.name,
    required this.primary,
    required this.surface,
  });
}

const List<AppPalette> appPalettes = [
  AppPalette(name: 'Pokédex Rouge', primary: Color(0xFFD32F2F), surface: Color(0xFF1B2333)),
  AppPalette(name: 'Océan Bleu',    primary: Color(0xFF1565C0), surface: Color(0xFF0D1B2A)),
  AppPalette(name: 'Forêt Verte',   primary: Color(0xFF2E7D32), surface: Color(0xFF1A2B1A)),
  AppPalette(name: 'Foudre Jaune',  primary: Color(0xFFF9A825), surface: Color(0xFF1C1A00)),
  AppPalette(name: 'Ombre Violette',primary: Color(0xFF6A1B9A), surface: Color(0xFF1A0A2B)),
];

const String _prefsPaletteIndex = 'palette_index';

class ThemeNotifier extends Notifier<int> {
  @override
  int build() {
    _loadSaved();
    return 0;
  }

  Future<void> _loadSaved() async {
    final prefs = await SharedPreferences.getInstance();
    final idx = prefs.getInt(_prefsPaletteIndex) ?? 0;
    state = idx.clamp(0, appPalettes.length - 1);
  }

  Future<void> setPalette(int index) async {
    state = index.clamp(0, appPalettes.length - 1);
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_prefsPaletteIndex, state);
  }

  AppPalette get current => appPalettes[state];
}

final themeProvider = NotifierProvider<ThemeNotifier, int>(ThemeNotifier.new);
