/**
 * CreateDatabase - Creates the SQLite database with a table and sample data
 * This example demonstrates how to create a SQLite database and insert rows.
 *
 * Part of Part III of the book "Introduction to Programming in Java"
 */

import java.sql.*;

public class CreateDatabase {

    private static final String DB_URL = "jdbc:sqlite:restaurants.db";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;

        try {
            // Connect to the database (creates the file if it does not exist)
            connection = DriverManager.getConnection(DB_URL);
            statement = connection.createStatement();

            // Create the restaurants table
            String createTableSQL = "CREATE TABLE IF NOT EXISTS restaurants (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT NOT NULL," +
                "price_range TEXT CHECK(price_range IN ('cheap', 'medium', 'expensive'))," +
                "area TEXT CHECK(area IN ('north', 'south', 'east', 'west', 'downtown'))," +
                "rating REAL CHECK(rating >= 0 AND rating <= 5)" +
                ")";

            statement.execute(createTableSQL);
            System.out.println("Table created successfully.");

            // Check whether data already exists
            ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM restaurants");
            if (rs.next() && rs.getInt(1) == 0) {
                // Insert sample data
                String[] insertStatements = {
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Pizza Place', 'medium', 'downtown', 4.5)",
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Sushi Corner', 'expensive', 'north', 4.8)",
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Prime Steakhouse', 'expensive', 'south', 4.7)",
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Corner Diner', 'cheap', 'east', 4.2)",
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Italian Bistro', 'medium', 'west', 4.6)",
                    "INSERT INTO restaurants (name, price_range, area, rating) VALUES ('Downtown Tavern', 'cheap', 'downtown', 4.3)"
                };

                for (String sql : insertStatements) {
                    statement.execute(sql);
                }
                System.out.println("Sample data inserted successfully.");
            } else {
                System.out.println("Data already exists in the database. Skipping insertion.");
            }

        } catch (SQLException e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            try {
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                System.err.println("Error closing resources: " + e.getMessage());
            }
        }
    }
}
