// lib/main.dart
// version: 1.5.1 (Fix: Class name conflict)
import 'package:flutter/material.dart';
import 'screens/pdf_list_screen.dart'; 
import 'models/invoice_models.dart'; 
import 'screens/invoice_input_screen.dart'; 
import 'screens/invoice_detail_page.dart';
import 'data/database.dart' as db; // エイリアスを追加

/// グローバルなデータベースインスタンス
late db.AppDatabase database;

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  database = db.AppDatabase();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '販売アシスト1号',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        useMaterial3: true,
      ),
      home: const InvoiceFlowScreen(),
    );
  }
}

class InvoiceFlowScreen extends StatefulWidget {
  const InvoiceFlowScreen({super.key});

  @override
  State<InvoiceFlowScreen> createState() => _InvoiceFlowScreenState();
}

class _InvoiceFlowScreenState extends State<InvoiceFlowScreen> {
  Invoice? _lastGeneratedInvoice;

  void _handleInvoiceGenerated(Invoice generatedInvoice, String filePath) {
    setState(() {
      _lastGeneratedInvoice = generatedInvoice;
    });

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => InvoiceDetailPage(invoice: generatedInvoice),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("販売アシスト1号 V1.5.1"),
        backgroundColor: Colors.blueGrey,
        actions: [
          IconButton(
            icon: const Icon(Icons.picture_as_pdf),
            tooltip: 'PDF一覧',
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (_) => const PdfListScreen()),
              );
            },
          ),
        ],
      ),
      body: InvoiceInputForm(onInvoiceGenerated: _handleInvoiceGenerated),
    );
  }
}